VirtualBox

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

Last change on this file since 1144 was 1113, checked in by vboxsync, 18 years ago

And again

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