VirtualBox

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

Last change on this file since 36147 was 36147, checked in by vboxsync, 14 years ago

rem: stdio.h build fix, 2nd try.

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

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