VirtualBox

source: vbox/trunk/src/recompiler/cpu-all.h@ 36125

最後變更 在這個檔案從36125是 36125,由 vboxsync 提交於 14 年 前

recompiler: Removing traces of attempts at making the recompiler compile with the microsoft compiler. (untested)

  • 屬性 svn:eol-style 設為 native
檔案大小: 33.3 KB
 
1/*
2 * defines common to all virtual CPUs
3 *
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
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
29
30#ifndef CPU_ALL_H
31#define CPU_ALL_H
32
33#ifdef VBOX
34# ifndef LOG_GROUP
35# define LOG_GROUP LOG_GROUP_REM
36# endif
37# include <VBox/log.h>
38# include <VBox/vmm/pgm.h> /* PGM_DYNAMIC_RAM_ALLOC */
39#endif
40
41#if defined(__arm__) || defined(__sparc__)
42#define WORDS_ALIGNED
43#endif
44
45/* some important defines:
46 *
47 * WORDS_ALIGNED : if defined, the host cpu can only make word aligned
48 * memory accesses.
49 *
50 * WORDS_BIGENDIAN : if defined, the host cpu is big endian and
51 * otherwise little endian.
52 *
53 * (TARGET_WORDS_ALIGNED : same for target cpu (not supported yet))
54 *
55 * TARGET_WORDS_BIGENDIAN : same for target cpu
56 */
57
58#include "bswap.h"
59
60#if defined(WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
61#define BSWAP_NEEDED
62#endif
63
64#ifdef BSWAP_NEEDED
65
66static inline uint16_t tswap16(uint16_t s)
67{
68 return bswap16(s);
69}
70
71static inline uint32_t tswap32(uint32_t s)
72{
73 return bswap32(s);
74}
75
76static inline uint64_t tswap64(uint64_t s)
77{
78 return bswap64(s);
79}
80
81static inline void tswap16s(uint16_t *s)
82{
83 *s = bswap16(*s);
84}
85
86static inline void tswap32s(uint32_t *s)
87{
88 *s = bswap32(*s);
89}
90
91static inline void tswap64s(uint64_t *s)
92{
93 *s = bswap64(*s);
94}
95
96#else
97
98static inline uint16_t tswap16(uint16_t s)
99{
100 return s;
101}
102
103static inline uint32_t tswap32(uint32_t s)
104{
105 return s;
106}
107
108static inline uint64_t tswap64(uint64_t s)
109{
110 return s;
111}
112
113static inline void tswap16s(uint16_t *s)
114{
115}
116
117static inline void tswap32s(uint32_t *s)
118{
119}
120
121static inline void tswap64s(uint64_t *s)
122{
123}
124
125#endif
126
127#if TARGET_LONG_SIZE == 4
128#define tswapl(s) tswap32(s)
129#define tswapls(s) tswap32s((uint32_t *)(s))
130#define bswaptls(s) bswap32s(s)
131#else
132#define tswapl(s) tswap64(s)
133#define tswapls(s) tswap64s((uint64_t *)(s))
134#define bswaptls(s) bswap64s(s)
135#endif
136
137typedef union {
138 float32 f;
139 uint32_t l;
140} CPU_FloatU;
141
142/* NOTE: arm FPA is horrible as double 32 bit words are stored in big
143 endian ! */
144typedef union {
145 float64 d;
146#if defined(WORDS_BIGENDIAN) \
147 || (defined(__arm__) && !defined(__VFP_FP__) && !defined(CONFIG_SOFTFLOAT))
148 struct {
149 uint32_t upper;
150 uint32_t lower;
151 } l;
152#else
153 struct {
154 uint32_t lower;
155 uint32_t upper;
156 } l;
157#endif
158 uint64_t ll;
159} CPU_DoubleU;
160
161#ifdef TARGET_SPARC
162typedef union {
163 float128 q;
164#if defined(WORDS_BIGENDIAN) \
165 || (defined(__arm__) && !defined(__VFP_FP__) && !defined(CONFIG_SOFTFLOAT))
166 struct {
167 uint32_t upmost;
168 uint32_t upper;
169 uint32_t lower;
170 uint32_t lowest;
171 } l;
172 struct {
173 uint64_t upper;
174 uint64_t lower;
175 } ll;
176#else
177 struct {
178 uint32_t lowest;
179 uint32_t lower;
180 uint32_t upper;
181 uint32_t upmost;
182 } l;
183 struct {
184 uint64_t lower;
185 uint64_t upper;
186 } ll;
187#endif
188} CPU_QuadU;
189#endif
190
191/* CPU memory access without any memory or io remapping */
192
193/*
194 * the generic syntax for the memory accesses is:
195 *
196 * load: ld{type}{sign}{size}{endian}_{access_type}(ptr)
197 *
198 * store: st{type}{size}{endian}_{access_type}(ptr, val)
199 *
200 * type is:
201 * (empty): integer access
202 * f : float access
203 *
204 * sign is:
205 * (empty): for floats or 32 bit size
206 * u : unsigned
207 * s : signed
208 *
209 * size is:
210 * b: 8 bits
211 * w: 16 bits
212 * l: 32 bits
213 * q: 64 bits
214 *
215 * endian is:
216 * (empty): target cpu endianness or 8 bit access
217 * r : reversed target cpu endianness (not implemented yet)
218 * be : big endian (not implemented yet)
219 * le : little endian (not implemented yet)
220 *
221 * access_type is:
222 * raw : host memory access
223 * user : user mode access using soft MMU
224 * kernel : kernel mode access using soft MMU
225 */
226
227#ifdef VBOX
228void remAbort(int rc, const char *pszTip) __attribute__((__noreturn__));
229
230void remR3PhysRead(RTGCPHYS SrcGCPhys, void *pvDst, unsigned cb);
231RTCCUINTREG remR3PhysReadU8(RTGCPHYS SrcGCPhys);
232RTCCINTREG remR3PhysReadS8(RTGCPHYS SrcGCPhys);
233RTCCUINTREG remR3PhysReadU16(RTGCPHYS SrcGCPhys);
234RTCCINTREG remR3PhysReadS16(RTGCPHYS SrcGCPhys);
235RTCCUINTREG remR3PhysReadU32(RTGCPHYS SrcGCPhys);
236RTCCINTREG remR3PhysReadS32(RTGCPHYS SrcGCPhys);
237uint64_t remR3PhysReadU64(RTGCPHYS SrcGCPhys);
238int64_t remR3PhysReadS64(RTGCPHYS SrcGCPhys);
239void remR3PhysWrite(RTGCPHYS DstGCPhys, const void *pvSrc, unsigned cb);
240void remR3PhysWriteU8(RTGCPHYS DstGCPhys, uint8_t val);
241void remR3PhysWriteU16(RTGCPHYS DstGCPhys, uint16_t val);
242void remR3PhysWriteU32(RTGCPHYS DstGCPhys, uint32_t val);
243void remR3PhysWriteU64(RTGCPHYS DstGCPhys, uint64_t val);
244
245#ifndef REM_PHYS_ADDR_IN_TLB
246void *remR3TlbGCPhys2Ptr(CPUState *env1, target_ulong physAddr, int fWritable);
247#endif
248
249#endif /* VBOX */
250
251#if defined(VBOX) && defined(REM_PHYS_ADDR_IN_TLB)
252
253DECLINLINE(uint8_t) ldub_p(void *ptr)
254{
255 VBOX_CHECK_ADDR(ptr);
256 return remR3PhysReadU8((uintptr_t)ptr);
257}
258
259DECLINLINE(int8_t) ldsb_p(void *ptr)
260{
261 VBOX_CHECK_ADDR(ptr);
262 return remR3PhysReadS8((uintptr_t)ptr);
263}
264
265DECLINLINE(void) stb_p(void *ptr, int v)
266{
267 VBOX_CHECK_ADDR(ptr);
268 remR3PhysWriteU8((uintptr_t)ptr, v);
269}
270
271DECLINLINE(uint32_t) lduw_le_p(void *ptr)
272{
273 VBOX_CHECK_ADDR(ptr);
274 return remR3PhysReadU16((uintptr_t)ptr);
275}
276
277DECLINLINE(int32_t) ldsw_le_p(void *ptr)
278{
279 VBOX_CHECK_ADDR(ptr);
280 return remR3PhysReadS16((uintptr_t)ptr);
281}
282
283DECLINLINE(void) stw_le_p(void *ptr, int v)
284{
285 VBOX_CHECK_ADDR(ptr);
286 remR3PhysWriteU16((uintptr_t)ptr, v);
287}
288
289DECLINLINE(uint32_t) ldl_le_p(void *ptr)
290{
291 VBOX_CHECK_ADDR(ptr);
292 return remR3PhysReadU32((uintptr_t)ptr);
293}
294
295DECLINLINE(void) stl_le_p(void *ptr, int v)
296{
297 VBOX_CHECK_ADDR(ptr);
298 remR3PhysWriteU32((uintptr_t)ptr, v);
299}
300
301DECLINLINE(void) stq_le_p(void *ptr, uint64_t v)
302{
303 VBOX_CHECK_ADDR(ptr);
304 remR3PhysWriteU64((uintptr_t)ptr, v);
305}
306
307DECLINLINE(uint64_t) ldq_le_p(void *ptr)
308{
309 VBOX_CHECK_ADDR(ptr);
310 return remR3PhysReadU64((uintptr_t)ptr);
311}
312
313#undef VBOX_CHECK_ADDR
314
315/* float access */
316
317DECLINLINE(float32) ldfl_le_p(void *ptr)
318{
319 union {
320 float32 f;
321 uint32_t i;
322 } u;
323 u.i = ldl_le_p(ptr);
324 return u.f;
325}
326
327DECLINLINE(void) stfl_le_p(void *ptr, float32 v)
328{
329 union {
330 float32 f;
331 uint32_t i;
332 } u;
333 u.f = v;
334 stl_le_p(ptr, u.i);
335}
336
337DECLINLINE(float64) ldfq_le_p(void *ptr)
338{
339 CPU_DoubleU u;
340 u.l.lower = ldl_le_p(ptr);
341 u.l.upper = ldl_le_p((uint8_t*)ptr + 4);
342 return u.d;
343}
344
345DECLINLINE(void) stfq_le_p(void *ptr, float64 v)
346{
347 CPU_DoubleU u;
348 u.d = v;
349 stl_le_p(ptr, u.l.lower);
350 stl_le_p((uint8_t*)ptr + 4, u.l.upper);
351}
352
353#else /* !VBOX */
354
355static inline int ldub_p(void *ptr)
356{
357 return *(uint8_t *)ptr;
358}
359
360static inline int ldsb_p(void *ptr)
361{
362 return *(int8_t *)ptr;
363}
364
365static inline void stb_p(void *ptr, int v)
366{
367 *(uint8_t *)ptr = v;
368}
369
370/* NOTE: on arm, putting 2 in /proc/sys/debug/alignment so that the
371 kernel handles unaligned load/stores may give better results, but
372 it is a system wide setting : bad */
373#if defined(WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
374
375/* conservative code for little endian unaligned accesses */
376static inline int lduw_le_p(void *ptr)
377{
378#ifdef __powerpc__
379 int val;
380 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
381 return val;
382#else
383 uint8_t *p = ptr;
384 return p[0] | (p[1] << 8);
385#endif
386}
387
388static inline int ldsw_le_p(void *ptr)
389{
390#ifdef __powerpc__
391 int val;
392 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
393 return (int16_t)val;
394#else
395 uint8_t *p = ptr;
396 return (int16_t)(p[0] | (p[1] << 8));
397#endif
398}
399
400static inline int ldl_le_p(void *ptr)
401{
402#ifdef __powerpc__
403 int val;
404 __asm__ __volatile__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (ptr));
405 return val;
406#else
407 uint8_t *p = ptr;
408 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
409#endif
410}
411
412static inline uint64_t ldq_le_p(void *ptr)
413{
414 uint8_t *p = ptr;
415 uint32_t v1, v2;
416 v1 = ldl_le_p(p);
417 v2 = ldl_le_p(p + 4);
418 return v1 | ((uint64_t)v2 << 32);
419}
420
421static inline void stw_le_p(void *ptr, int v)
422{
423#ifdef __powerpc__
424 __asm__ __volatile__ ("sthbrx %1,0,%2" : "=m" (*(uint16_t *)ptr) : "r" (v), "r" (ptr));
425#else
426 uint8_t *p = ptr;
427 p[0] = v;
428 p[1] = v >> 8;
429#endif
430}
431
432static inline void stl_le_p(void *ptr, int v)
433{
434#ifdef __powerpc__
435 __asm__ __volatile__ ("stwbrx %1,0,%2" : "=m" (*(uint32_t *)ptr) : "r" (v), "r" (ptr));
436#else
437 uint8_t *p = ptr;
438 p[0] = v;
439 p[1] = v >> 8;
440 p[2] = v >> 16;
441 p[3] = v >> 24;
442#endif
443}
444
445static inline void stq_le_p(void *ptr, uint64_t v)
446{
447 uint8_t *p = ptr;
448 stl_le_p(p, (uint32_t)v);
449 stl_le_p(p + 4, v >> 32);
450}
451
452/* float access */
453
454static inline float32 ldfl_le_p(void *ptr)
455{
456 union {
457 float32 f;
458 uint32_t i;
459 } u;
460 u.i = ldl_le_p(ptr);
461 return u.f;
462}
463
464static inline void stfl_le_p(void *ptr, float32 v)
465{
466 union {
467 float32 f;
468 uint32_t i;
469 } u;
470 u.f = v;
471 stl_le_p(ptr, u.i);
472}
473
474static inline float64 ldfq_le_p(void *ptr)
475{
476 CPU_DoubleU u;
477 u.l.lower = ldl_le_p(ptr);
478 u.l.upper = ldl_le_p(ptr + 4);
479 return u.d;
480}
481
482static inline void stfq_le_p(void *ptr, float64 v)
483{
484 CPU_DoubleU u;
485 u.d = v;
486 stl_le_p(ptr, u.l.lower);
487 stl_le_p(ptr + 4, u.l.upper);
488}
489
490#else
491
492static inline int lduw_le_p(void *ptr)
493{
494 return *(uint16_t *)ptr;
495}
496
497static inline int ldsw_le_p(void *ptr)
498{
499 return *(int16_t *)ptr;
500}
501
502static inline int ldl_le_p(void *ptr)
503{
504 return *(uint32_t *)ptr;
505}
506
507static inline uint64_t ldq_le_p(void *ptr)
508{
509 return *(uint64_t *)ptr;
510}
511
512static inline void stw_le_p(void *ptr, int v)
513{
514 *(uint16_t *)ptr = v;
515}
516
517static inline void stl_le_p(void *ptr, int v)
518{
519 *(uint32_t *)ptr = v;
520}
521
522static inline void stq_le_p(void *ptr, uint64_t v)
523{
524 *(uint64_t *)ptr = v;
525}
526
527/* float access */
528
529static inline float32 ldfl_le_p(void *ptr)
530{
531 return *(float32 *)ptr;
532}
533
534static inline float64 ldfq_le_p(void *ptr)
535{
536 return *(float64 *)ptr;
537}
538
539static inline void stfl_le_p(void *ptr, float32 v)
540{
541 *(float32 *)ptr = v;
542}
543
544static inline void stfq_le_p(void *ptr, float64 v)
545{
546 *(float64 *)ptr = v;
547}
548#endif
549#endif /* !VBOX */
550
551#if !defined(WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
552
553static inline int lduw_be_p(void *ptr)
554{
555#if defined(__i386__)
556 int val;
557 asm volatile ("movzwl %1, %0\n"
558 "xchgb %b0, %h0\n"
559 : "=q" (val)
560 : "m" (*(uint16_t *)ptr));
561 return val;
562#else
563 uint8_t *b = (uint8_t *) ptr;
564 return ((b[0] << 8) | b[1]);
565#endif
566}
567
568static inline int ldsw_be_p(void *ptr)
569{
570#if defined(__i386__)
571 int val;
572 asm volatile ("movzwl %1, %0\n"
573 "xchgb %b0, %h0\n"
574 : "=q" (val)
575 : "m" (*(uint16_t *)ptr));
576 return (int16_t)val;
577#else
578 uint8_t *b = (uint8_t *) ptr;
579 return (int16_t)((b[0] << 8) | b[1]);
580#endif
581}
582
583static inline int ldl_be_p(void *ptr)
584{
585#if defined(__i386__) || defined(__x86_64__)
586 int val;
587 asm volatile ("movl %1, %0\n"
588 "bswap %0\n"
589 : "=r" (val)
590 : "m" (*(uint32_t *)ptr));
591 return val;
592#else
593 uint8_t *b = (uint8_t *) ptr;
594 return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
595#endif
596}
597
598static inline uint64_t ldq_be_p(void *ptr)
599{
600 uint32_t a,b;
601 a = ldl_be_p(ptr);
602 b = ldl_be_p((uint8_t*)ptr+4);
603 return (((uint64_t)a<<32)|b);
604}
605
606static inline void stw_be_p(void *ptr, int v)
607{
608#if defined(__i386__)
609 asm volatile ("xchgb %b0, %h0\n"
610 "movw %w0, %1\n"
611 : "=q" (v)
612 : "m" (*(uint16_t *)ptr), "0" (v));
613#else
614 uint8_t *d = (uint8_t *) ptr;
615 d[0] = v >> 8;
616 d[1] = v;
617#endif
618}
619
620static inline void stl_be_p(void *ptr, int v)
621{
622#if defined(__i386__) || defined(__x86_64__)
623 asm volatile ("bswap %0\n"
624 "movl %0, %1\n"
625 : "=r" (v)
626 : "m" (*(uint32_t *)ptr), "0" (v));
627#else
628 uint8_t *d = (uint8_t *) ptr;
629 d[0] = v >> 24;
630 d[1] = v >> 16;
631 d[2] = v >> 8;
632 d[3] = v;
633#endif
634}
635
636static inline void stq_be_p(void *ptr, uint64_t v)
637{
638 stl_be_p(ptr, v >> 32);
639 stl_be_p((uint8_t*)ptr + 4, v);
640}
641
642/* float access */
643static inline float32 ldfl_be_p(void *ptr)
644{
645 union {
646 float32 f;
647 uint32_t i;
648 } u;
649 u.i = ldl_be_p(ptr);
650 return u.f;
651}
652
653static inline void stfl_be_p(void *ptr, float32 v)
654{
655 union {
656 float32 f;
657 uint32_t i;
658 } u;
659 u.f = v;
660 stl_be_p(ptr, u.i);
661}
662
663static inline float64 ldfq_be_p(void *ptr)
664{
665 CPU_DoubleU u;
666 u.l.upper = ldl_be_p(ptr);
667 u.l.lower = ldl_be_p((uint8_t*)ptr + 4);
668 return u.d;
669}
670
671static inline void stfq_be_p(void *ptr, float64 v)
672{
673 CPU_DoubleU u;
674 u.d = v;
675 stl_be_p(ptr, u.l.upper);
676 stl_be_p((uint8_t*)ptr + 4, u.l.lower);
677}
678
679#else
680
681static inline int lduw_be_p(void *ptr)
682{
683 return *(uint16_t *)ptr;
684}
685
686static inline int ldsw_be_p(void *ptr)
687{
688 return *(int16_t *)ptr;
689}
690
691static inline int ldl_be_p(void *ptr)
692{
693 return *(uint32_t *)ptr;
694}
695
696static inline uint64_t ldq_be_p(void *ptr)
697{
698 return *(uint64_t *)ptr;
699}
700
701static inline void stw_be_p(void *ptr, int v)
702{
703 *(uint16_t *)ptr = v;
704}
705
706static inline void stl_be_p(void *ptr, int v)
707{
708 *(uint32_t *)ptr = v;
709}
710
711static inline void stq_be_p(void *ptr, uint64_t v)
712{
713 *(uint64_t *)ptr = v;
714}
715
716/* float access */
717
718static inline float32 ldfl_be_p(void *ptr)
719{
720 return *(float32 *)ptr;
721}
722
723static inline float64 ldfq_be_p(void *ptr)
724{
725 return *(float64 *)ptr;
726}
727
728static inline void stfl_be_p(void *ptr, float32 v)
729{
730 *(float32 *)ptr = v;
731}
732
733static inline void stfq_be_p(void *ptr, float64 v)
734{
735 *(float64 *)ptr = v;
736}
737
738#endif
739
740/* target CPU memory access functions */
741#if defined(TARGET_WORDS_BIGENDIAN)
742#define lduw_p(p) lduw_be_p(p)
743#define ldsw_p(p) ldsw_be_p(p)
744#define ldl_p(p) ldl_be_p(p)
745#define ldq_p(p) ldq_be_p(p)
746#define ldfl_p(p) ldfl_be_p(p)
747#define ldfq_p(p) ldfq_be_p(p)
748#define stw_p(p, v) stw_be_p(p, v)
749#define stl_p(p, v) stl_be_p(p, v)
750#define stq_p(p, v) stq_be_p(p, v)
751#define stfl_p(p, v) stfl_be_p(p, v)
752#define stfq_p(p, v) stfq_be_p(p, v)
753#else
754#define lduw_p(p) lduw_le_p(p)
755#define ldsw_p(p) ldsw_le_p(p)
756#define ldl_p(p) ldl_le_p(p)
757#define ldq_p(p) ldq_le_p(p)
758#define ldfl_p(p) ldfl_le_p(p)
759#define ldfq_p(p) ldfq_le_p(p)
760#define stw_p(p, v) stw_le_p(p, v)
761#define stl_p(p, v) stl_le_p(p, v)
762#define stq_p(p, v) stq_le_p(p, v)
763#define stfl_p(p, v) stfl_le_p(p, v)
764#define stfq_p(p, v) stfq_le_p(p, v)
765#endif
766
767/* MMU memory access macros */
768
769#if defined(CONFIG_USER_ONLY)
770/* On some host systems the guest address space is reserved on the host.
771 * This allows the guest address space to be offset to a convenient location.
772 */
773//#define GUEST_BASE 0x20000000
774#define GUEST_BASE 0
775
776/* All direct uses of g2h and h2g need to go away for usermode softmmu. */
777#define g2h(x) ((void *)((unsigned long)(x) + GUEST_BASE))
778#define h2g(x) ((target_ulong)(x - GUEST_BASE))
779#define saddr(x) g2h(x)
780#define laddr(x) g2h(x)
781
782#else /* !CONFIG_USER_ONLY */
783/* NOTE: we use double casts if pointers and target_ulong have
784 different sizes */
785#define saddr(x) (uint8_t *)(long)(x)
786#define laddr(x) (uint8_t *)(long)(x)
787#endif
788
789#define ldub_raw(p) ldub_p(laddr((p)))
790#define ldsb_raw(p) ldsb_p(laddr((p)))
791#define lduw_raw(p) lduw_p(laddr((p)))
792#define ldsw_raw(p) ldsw_p(laddr((p)))
793#define ldl_raw(p) ldl_p(laddr((p)))
794#define ldq_raw(p) ldq_p(laddr((p)))
795#define ldfl_raw(p) ldfl_p(laddr((p)))
796#define ldfq_raw(p) ldfq_p(laddr((p)))
797#define stb_raw(p, v) stb_p(saddr((p)), v)
798#define stw_raw(p, v) stw_p(saddr((p)), v)
799#define stl_raw(p, v) stl_p(saddr((p)), v)
800#define stq_raw(p, v) stq_p(saddr((p)), v)
801#define stfl_raw(p, v) stfl_p(saddr((p)), v)
802#define stfq_raw(p, v) stfq_p(saddr((p)), v)
803
804
805#if defined(CONFIG_USER_ONLY)
806
807/* if user mode, no other memory access functions */
808#define ldub(p) ldub_raw(p)
809#define ldsb(p) ldsb_raw(p)
810#define lduw(p) lduw_raw(p)
811#define ldsw(p) ldsw_raw(p)
812#define ldl(p) ldl_raw(p)
813#define ldq(p) ldq_raw(p)
814#define ldfl(p) ldfl_raw(p)
815#define ldfq(p) ldfq_raw(p)
816#define stb(p, v) stb_raw(p, v)
817#define stw(p, v) stw_raw(p, v)
818#define stl(p, v) stl_raw(p, v)
819#define stq(p, v) stq_raw(p, v)
820#define stfl(p, v) stfl_raw(p, v)
821#define stfq(p, v) stfq_raw(p, v)
822
823#define ldub_code(p) ldub_raw(p)
824#define ldsb_code(p) ldsb_raw(p)
825#define lduw_code(p) lduw_raw(p)
826#define ldsw_code(p) ldsw_raw(p)
827#define ldl_code(p) ldl_raw(p)
828
829#define ldub_kernel(p) ldub_raw(p)
830#define ldsb_kernel(p) ldsb_raw(p)
831#define lduw_kernel(p) lduw_raw(p)
832#define ldsw_kernel(p) ldsw_raw(p)
833#define ldl_kernel(p) ldl_raw(p)
834#define ldfl_kernel(p) ldfl_raw(p)
835#define ldfq_kernel(p) ldfq_raw(p)
836#define stb_kernel(p, v) stb_raw(p, v)
837#define stw_kernel(p, v) stw_raw(p, v)
838#define stl_kernel(p, v) stl_raw(p, v)
839#define stq_kernel(p, v) stq_raw(p, v)
840#define stfl_kernel(p, v) stfl_raw(p, v)
841#define stfq_kernel(p, vt) stfq_raw(p, v)
842
843#endif /* defined(CONFIG_USER_ONLY) */
844
845/* page related stuff */
846
847#define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
848#define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
849#define TARGET_PAGE_ALIGN(addr) (((addr) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK)
850
851/* ??? These should be the larger of unsigned long and target_ulong. */
852extern unsigned long qemu_real_host_page_size;
853extern unsigned long qemu_host_page_bits;
854extern unsigned long qemu_host_page_size;
855extern unsigned long qemu_host_page_mask;
856
857#define HOST_PAGE_ALIGN(addr) (((addr) + qemu_host_page_size - 1) & qemu_host_page_mask)
858
859/* same as PROT_xxx */
860#define PAGE_READ 0x0001
861#define PAGE_WRITE 0x0002
862#define PAGE_EXEC 0x0004
863#define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
864#define PAGE_VALID 0x0008
865/* original state of the write flag (used when tracking self-modifying
866 code */
867#define PAGE_WRITE_ORG 0x0010
868#define PAGE_RESERVED 0x0020
869
870void page_dump(FILE *f);
871int page_get_flags(target_ulong address);
872void page_set_flags(target_ulong start, target_ulong end, int flags);
873int page_check_range(target_ulong start, target_ulong len, int flags);
874void page_unprotect_range(target_ulong data, target_ulong data_size);
875
876#define SINGLE_CPU_DEFINES
877#ifdef SINGLE_CPU_DEFINES
878
879#if defined(TARGET_I386)
880
881#define CPUState CPUX86State
882#define cpu_init cpu_x86_init
883#define cpu_exec cpu_x86_exec
884#define cpu_gen_code cpu_x86_gen_code
885#define cpu_signal_handler cpu_x86_signal_handler
886
887#elif defined(TARGET_ARM)
888
889#define CPUState CPUARMState
890#define cpu_init cpu_arm_init
891#define cpu_exec cpu_arm_exec
892#define cpu_gen_code cpu_arm_gen_code
893#define cpu_signal_handler cpu_arm_signal_handler
894
895#elif defined(TARGET_SPARC)
896
897#define CPUState CPUSPARCState
898#define cpu_init cpu_sparc_init
899#define cpu_exec cpu_sparc_exec
900#define cpu_gen_code cpu_sparc_gen_code
901#define cpu_signal_handler cpu_sparc_signal_handler
902
903#elif defined(TARGET_PPC)
904
905#define CPUState CPUPPCState
906#define cpu_init cpu_ppc_init
907#define cpu_exec cpu_ppc_exec
908#define cpu_gen_code cpu_ppc_gen_code
909#define cpu_signal_handler cpu_ppc_signal_handler
910
911#elif defined(TARGET_M68K)
912#define CPUState CPUM68KState
913#define cpu_init cpu_m68k_init
914#define cpu_exec cpu_m68k_exec
915#define cpu_gen_code cpu_m68k_gen_code
916#define cpu_signal_handler cpu_m68k_signal_handler
917
918#elif defined(TARGET_MIPS)
919#define CPUState CPUMIPSState
920#define cpu_init cpu_mips_init
921#define cpu_exec cpu_mips_exec
922#define cpu_gen_code cpu_mips_gen_code
923#define cpu_signal_handler cpu_mips_signal_handler
924
925#elif defined(TARGET_SH4)
926#define CPUState CPUSH4State
927#define cpu_init cpu_sh4_init
928#define cpu_exec cpu_sh4_exec
929#define cpu_gen_code cpu_sh4_gen_code
930#define cpu_signal_handler cpu_sh4_signal_handler
931
932#else
933
934#error unsupported target CPU
935
936#endif
937
938#endif /* SINGLE_CPU_DEFINES */
939
940void cpu_dump_state(CPUState *env, FILE *f,
941 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
942 int flags);
943
944DECLNORETURN(void) cpu_abort(CPUState *env, const char *fmt, ...);
945extern CPUState *first_cpu;
946extern CPUState *cpu_single_env;
947extern int64_t qemu_icount;
948extern int use_icount;
949
950#define CPU_INTERRUPT_EXIT 0x01 /* wants exit from main loop */
951#define CPU_INTERRUPT_HARD 0x02 /* hardware interrupt pending */
952#define CPU_INTERRUPT_EXITTB 0x04 /* exit the current TB (use for x86 a20 case) */
953#define CPU_INTERRUPT_TIMER 0x08 /* internal timer exception pending */
954#define CPU_INTERRUPT_FIQ 0x10 /* Fast interrupt pending. */
955#define CPU_INTERRUPT_HALT 0x20 /* CPU halt wanted */
956#define CPU_INTERRUPT_SMI 0x40 /* (x86 only) SMI interrupt pending */
957#define CPU_INTERRUPT_DEBUG 0x80 /* Debug event occurred. */
958#define CPU_INTERRUPT_VIRQ 0x100 /* virtual interrupt pending. */
959#define CPU_INTERRUPT_NMI 0x200 /* NMI pending. */
960
961#ifdef VBOX
962/** Executes a single instruction. cpu_exec() will normally return EXCP_SINGLE_INSTR. */
963#define CPU_INTERRUPT_SINGLE_INSTR 0x0400
964/** Executing a CPU_INTERRUPT_SINGLE_INSTR request, quit the cpu_loop. (for exceptions and suchlike) */
965#define CPU_INTERRUPT_SINGLE_INSTR_IN_FLIGHT 0x0800
966/** VM execution was interrupted by VMR3Reset, VMR3Suspend or VMR3PowerOff. */
967#define CPU_INTERRUPT_RC 0x1000
968/** Exit current TB to process an external interrupt request (also in op.c!!) */
969#define CPU_INTERRUPT_EXTERNAL_EXIT 0x2000
970/** Exit current TB to process an external interrupt request (also in op.c!!) */
971#define CPU_INTERRUPT_EXTERNAL_HARD 0x4000
972/** Exit current TB to process an external interrupt request (also in op.c!!) */
973#define CPU_INTERRUPT_EXTERNAL_TIMER 0x8000
974/** Exit current TB to process an external interrupt request (also in op.c!!) */
975#define CPU_INTERRUPT_EXTERNAL_DMA 0x10000
976#endif /* VBOX */
977void cpu_interrupt(CPUState *s, int mask);
978void cpu_reset_interrupt(CPUState *env, int mask);
979
980int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type);
981int cpu_watchpoint_remove(CPUState *env, target_ulong addr);
982void cpu_watchpoint_remove_all(CPUState *env);
983int cpu_breakpoint_insert(CPUState *env, target_ulong pc);
984int cpu_breakpoint_remove(CPUState *env, target_ulong pc);
985void cpu_breakpoint_remove_all(CPUState *env);
986
987#define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
988#define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
989#define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
990
991void cpu_single_step(CPUState *env, int enabled);
992void cpu_reset(CPUState *s);
993
994/* Return the physical page corresponding to a virtual one. Use it
995 only for debugging because no protection checks are done. Return -1
996 if no page found. */
997target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr);
998
999#define CPU_LOG_TB_OUT_ASM (1 << 0)
1000#define CPU_LOG_TB_IN_ASM (1 << 1)
1001#define CPU_LOG_TB_OP (1 << 2)
1002#define CPU_LOG_TB_OP_OPT (1 << 3)
1003#define CPU_LOG_INT (1 << 4)
1004#define CPU_LOG_EXEC (1 << 5)
1005#define CPU_LOG_PCALL (1 << 6)
1006#define CPU_LOG_IOPORT (1 << 7)
1007#define CPU_LOG_TB_CPU (1 << 8)
1008
1009/* define log items */
1010typedef struct CPULogItem {
1011 int mask;
1012 const char *name;
1013 const char *help;
1014} CPULogItem;
1015
1016extern CPULogItem cpu_log_items[];
1017
1018void cpu_set_log(int log_flags);
1019void cpu_set_log_filename(const char *filename);
1020int cpu_str_to_log_mask(const char *str);
1021
1022/* IO ports API */
1023
1024/* NOTE: as these functions may be even used when there is an isa
1025 brige on non x86 targets, we always defined them */
1026#ifndef NO_CPU_IO_DEFS
1027void cpu_outb(CPUState *env, int addr, int val);
1028void cpu_outw(CPUState *env, int addr, int val);
1029void cpu_outl(CPUState *env, int addr, int val);
1030int cpu_inb(CPUState *env, int addr);
1031int cpu_inw(CPUState *env, int addr);
1032int cpu_inl(CPUState *env, int addr);
1033#endif
1034
1035/* address in the RAM (different from a physical address) */
1036#ifdef USE_KQEMU
1037typedef uint32_t ram_addr_t;
1038#else
1039typedef unsigned long ram_addr_t;
1040#endif
1041
1042/* memory API */
1043
1044#ifndef VBOX
1045extern int phys_ram_size;
1046extern int phys_ram_fd;
1047extern int phys_ram_size;
1048#else /* VBOX */
1049extern RTGCPHYS phys_ram_size;
1050/** This is required for bounds checking the phys_ram_dirty accesses. */
1051extern RTGCPHYS phys_ram_dirty_size;
1052#endif /* VBOX */
1053#if !defined(VBOX)
1054extern uint8_t *phys_ram_base;
1055#endif
1056extern uint8_t *phys_ram_dirty;
1057
1058/* physical memory access */
1059
1060/* MMIO pages are identified by a combination of an IO device index and
1061 3 flags. The ROMD code stores the page ram offset in iotlb entry,
1062 so only a limited number of ids are avaiable. */
1063
1064#define IO_MEM_SHIFT 3
1065#define IO_MEM_NB_ENTRIES (1 << (TARGET_PAGE_BITS - IO_MEM_SHIFT))
1066
1067#define IO_MEM_RAM (0 << IO_MEM_SHIFT) /* hardcoded offset */
1068#define IO_MEM_ROM (1 << IO_MEM_SHIFT) /* hardcoded offset */
1069#define IO_MEM_UNASSIGNED (2 << IO_MEM_SHIFT)
1070#define IO_MEM_NOTDIRTY (3 << IO_MEM_SHIFT)
1071
1072/* Acts like a ROM when read and like a device when written. */
1073#define IO_MEM_ROMD (1)
1074#define IO_MEM_SUBPAGE (2)
1075#define IO_MEM_SUBWIDTH (4)
1076
1077/* Flags stored in the low bits of the TLB virtual address. These are
1078 defined so that fast path ram access is all zeros. */
1079/* Zero if TLB entry is valid. */
1080#define TLB_INVALID_MASK (1 << 3)
1081/* Set if TLB entry references a clean RAM page. The iotlb entry will
1082 contain the page physical address. */
1083#define TLB_NOTDIRTY (1 << 4)
1084/* Set if TLB entry is an IO callback. */
1085#define TLB_MMIO (1 << 5)
1086
1087typedef void CPUWriteMemoryFunc(void *opaque, target_phys_addr_t addr, uint32_t value);
1088typedef uint32_t CPUReadMemoryFunc(void *opaque, target_phys_addr_t addr);
1089
1090void cpu_register_physical_memory(target_phys_addr_t start_addr,
1091 ram_addr_t size,
1092 ram_addr_t phys_offset);
1093uint32_t cpu_get_physical_page_desc(target_phys_addr_t addr);
1094ram_addr_t qemu_ram_alloc(ram_addr_t);
1095void qemu_ram_free(ram_addr_t addr);
1096int cpu_register_io_memory(int io_index,
1097 CPUReadMemoryFunc **mem_read,
1098 CPUWriteMemoryFunc **mem_write,
1099 void *opaque);
1100CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index);
1101CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index);
1102
1103void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
1104 int len, int is_write);
1105static inline void cpu_physical_memory_read(target_phys_addr_t addr,
1106 uint8_t *buf, int len)
1107{
1108 cpu_physical_memory_rw(addr, buf, len, 0);
1109}
1110static inline void cpu_physical_memory_write(target_phys_addr_t addr,
1111 const uint8_t *buf, int len)
1112{
1113 cpu_physical_memory_rw(addr, (uint8_t *)buf, len, 1);
1114}
1115uint32_t ldub_phys(target_phys_addr_t addr);
1116uint32_t lduw_phys(target_phys_addr_t addr);
1117uint32_t ldl_phys(target_phys_addr_t addr);
1118uint64_t ldq_phys(target_phys_addr_t addr);
1119void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val);
1120void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val);
1121void stb_phys(target_phys_addr_t addr, uint32_t val);
1122void stw_phys(target_phys_addr_t addr, uint32_t val);
1123void stl_phys(target_phys_addr_t addr, uint32_t val);
1124void stq_phys(target_phys_addr_t addr, uint64_t val);
1125
1126void cpu_physical_memory_write_rom(target_phys_addr_t addr,
1127 const uint8_t *buf, int len);
1128int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
1129 uint8_t *buf, int len, int is_write);
1130
1131#define VGA_DIRTY_FLAG 0x01
1132#define CODE_DIRTY_FLAG 0x02
1133#define KQEMU_DIRTY_FLAG 0x04
1134#define MIGRATION_DIRTY_FLAG 0x08
1135
1136/* read dirty bit (return 0 or 1) */
1137#ifndef VBOX
1138static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
1139{
1140 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
1141}
1142#else
1143DECLINLINE(int) cpu_physical_memory_is_dirty(ram_addr_t addr)
1144{
1145 if (RT_UNLIKELY((addr >> TARGET_PAGE_BITS) >= phys_ram_dirty_size))
1146 {
1147 Log(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));
1148 /*AssertMsgFailed(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));*/
1149 return 0;
1150 }
1151 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
1152}
1153#endif
1154
1155#ifndef VBOX
1156static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
1157 int dirty_flags)
1158{
1159 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
1160}
1161#else
1162DECLINLINE(int) cpu_physical_memory_get_dirty(ram_addr_t addr,
1163 int dirty_flags)
1164{
1165 if (RT_UNLIKELY((addr >> TARGET_PAGE_BITS) >= phys_ram_dirty_size))
1166 {
1167 Log(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));
1168 /*AssertMsgFailed(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));*/
1169 return 0xff & dirty_flags; /** @todo I don't think this is the right thing to return, fix! */
1170 }
1171 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
1172}
1173#endif
1174
1175#ifndef VBOX
1176static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
1177{
1178 phys_ram_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
1179}
1180#else
1181DECLINLINE(void) cpu_physical_memory_set_dirty(ram_addr_t addr)
1182{
1183 if (RT_UNLIKELY((addr >> TARGET_PAGE_BITS) >= phys_ram_dirty_size))
1184 {
1185 Log(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));
1186 /*AssertMsgFailed(("cpu_physical_memory_is_dirty: %RGp\n", (RTGCPHYS)addr));*/
1187 return;
1188 }
1189 phys_ram_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
1190}
1191#endif
1192
1193void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
1194 int dirty_flags);
1195void cpu_tlb_update_dirty(CPUState *env);
1196
1197int cpu_physical_memory_set_dirty_tracking(int enable);
1198
1199int cpu_physical_memory_get_dirty_tracking(void);
1200
1201void dump_exec_info(FILE *f,
1202 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
1203
1204/*******************************************/
1205/* host CPU ticks (if available) */
1206
1207#ifdef VBOX
1208# include <iprt/asm-amd64-x86.h>
1209
1210DECLINLINE(int64_t) cpu_get_real_ticks(void)
1211{
1212 return ASMReadTSC();
1213}
1214
1215#elif defined(__powerpc__)
1216
1217static inline uint32_t get_tbl(void)
1218{
1219 uint32_t tbl;
1220 asm volatile("mftb %0" : "=r" (tbl));
1221 return tbl;
1222}
1223
1224static inline uint32_t get_tbu(void)
1225{
1226 uint32_t tbl;
1227 asm volatile("mftbu %0" : "=r" (tbl));
1228 return tbl;
1229}
1230
1231static inline int64_t cpu_get_real_ticks(void)
1232{
1233 uint32_t l, h, h1;
1234 /* NOTE: we test if wrapping has occurred */
1235 do {
1236 h = get_tbu();
1237 l = get_tbl();
1238 h1 = get_tbu();
1239 } while (h != h1);
1240 return ((int64_t)h << 32) | l;
1241}
1242
1243#elif defined(__i386__)
1244
1245static inline int64_t cpu_get_real_ticks(void)
1246{
1247 int64_t val;
1248 asm volatile ("rdtsc" : "=A" (val));
1249 return val;
1250}
1251
1252#elif defined(__x86_64__)
1253
1254static inline int64_t cpu_get_real_ticks(void)
1255{
1256 uint32_t low,high;
1257 int64_t val;
1258 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1259 val = high;
1260 val <<= 32;
1261 val |= low;
1262 return val;
1263}
1264
1265#elif defined(__ia64)
1266
1267static inline int64_t cpu_get_real_ticks(void)
1268{
1269 int64_t val;
1270 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
1271 return val;
1272}
1273
1274#elif defined(__s390__)
1275
1276static inline int64_t cpu_get_real_ticks(void)
1277{
1278 int64_t val;
1279 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
1280 return val;
1281}
1282
1283#elif defined(__sparc_v9__)
1284
1285static inline int64_t cpu_get_real_ticks (void)
1286{
1287#if defined(_LP64)
1288 uint64_t rval;
1289 asm volatile("rd %%tick,%0" : "=r"(rval));
1290 return rval;
1291#else
1292 union {
1293 uint64_t i64;
1294 struct {
1295 uint32_t high;
1296 uint32_t low;
1297 } i32;
1298 } rval;
1299 asm volatile("rd %%tick,%1; srlx %1,32,%0"
1300 : "=r"(rval.i32.high), "=r"(rval.i32.low));
1301 return rval.i64;
1302#endif
1303}
1304#else
1305/* The host CPU doesn't have an easily accessible cycle counter.
1306 Just return a monotonically increasing vlue. This will be totally wrong,
1307 but hopefully better than nothing. */
1308static inline int64_t cpu_get_real_ticks (void)
1309{
1310 static int64_t ticks = 0;
1311 return ticks++;
1312}
1313#endif
1314
1315/* profiling */
1316#ifdef CONFIG_PROFILER
1317static inline int64_t profile_getclock(void)
1318{
1319 return cpu_get_real_ticks();
1320}
1321
1322extern int64_t kqemu_time, kqemu_time_start;
1323extern int64_t qemu_time, qemu_time_start;
1324extern int64_t tlb_flush_time;
1325extern int64_t kqemu_exec_count;
1326extern int64_t dev_time;
1327extern int64_t kqemu_ret_int_count;
1328extern int64_t kqemu_ret_excp_count;
1329extern int64_t kqemu_ret_intr_count;
1330#endif
1331
1332#ifdef VBOX
1333void tb_invalidate_virt(CPUState *env, uint32_t eip);
1334#endif /* VBOX */
1335
1336#endif /* CPU_ALL_H */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette