VirtualBox

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

Last change on this file since 1111 was 1109, checked in by vboxsync, 18 years ago

compile fix

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

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