ppc fixes (Jocelyn Mayer)
[qemu] / target-ppc / cpu.h
1 /*
2  *  PPC emulation cpu definitions for qemu.
3  * 
4  *  Copyright (c) 2003 Jocelyn Mayer
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 #if !defined (__CPU_PPC_H__)
21 #define __CPU_PPC_H__
22
23 #define TARGET_LONG_BITS 32
24
25 #include "cpu-defs.h"
26
27 #include "config.h"
28 #include <setjmp.h>
29
30 /* Instruction types */
31 enum {
32     PPC_NONE     = 0x0000,
33     PPC_INTEGER  = 0x0001, /* CPU has integer operations instructions        */
34     PPC_FLOAT    = 0x0002, /* CPU has floating point operations instructions */
35     PPC_FLOW     = 0x0004, /* CPU has flow control instructions              */
36     PPC_MEM      = 0x0008, /* CPU has virtual memory instructions            */
37     PPC_RES      = 0x0010, /* CPU has ld/st with reservation instructions    */
38     PPC_CACHE    = 0x0020, /* CPU has cache control instructions             */
39     PPC_MISC     = 0x0040, /* CPU has spr/msr access instructions            */
40     PPC_EXTERN   = 0x0080, /* CPU has external control instructions          */
41     PPC_SEGMENT  = 0x0100, /* CPU has memory segment instructions            */
42     PPC_CACHE_OPT= 0x0200,
43     PPC_FLOAT_OPT= 0x0400,
44     PPC_MEM_OPT  = 0x0800,
45 };
46
47 #define PPC_COMMON  (PPC_INTEGER | PPC_FLOAT | PPC_FLOW | PPC_MEM |           \
48                      PPC_RES | PPC_CACHE | PPC_MISC | PPC_SEGMENT)
49 /* PPC 604 */
50 #define PPC_604 (PPC_INTEGER | PPC_FLOAT | PPC_FLOW | PPC_MEM |               \
51                  PPC_RES | PPC_CACHE | PPC_MISC | PPC_EXTERN | PPC_SEGMENT    \
52                  PPC_MEM_OPT)
53 /* PPC 740/745/750/755 (aka G3) has external access instructions */
54 #define PPC_750 (PPC_INTEGER | PPC_FLOAT | PPC_FLOW | PPC_MEM |               \
55                  PPC_RES | PPC_CACHE | PPC_MISC | PPC_EXTERN | PPC_SEGMENT)
56
57 typedef struct ppc_tb_t ppc_tb_t;
58
59 /* Supervisor mode registers */
60 /* Machine state register */
61 #define MSR_POW 18
62 #define MSR_ILE 16
63 #define MSR_EE  15
64 #define MSR_PR  14
65 #define MSR_FP  13
66 #define MSR_ME  12
67 #define MSR_FE0 11
68 #define MSR_SE  10
69 #define MSR_BE  9
70 #define MSR_FE1 8
71 #define MSR_IP 6
72 #define MSR_IR 5
73 #define MSR_DR 4
74 #define MSR_RI 1
75 #define MSR_LE 0
76 #define msr_pow env->msr[MSR_POW]
77 #define msr_ile env->msr[MSR_ILE]
78 #define msr_ee  env->msr[MSR_EE]
79 #define msr_pr  env->msr[MSR_PR]
80 #define msr_fp  env->msr[MSR_FP]
81 #define msr_me  env->msr[MSR_ME]
82 #define msr_fe0 env->msr[MSR_FE0]
83 #define msr_se  env->msr[MSR_SE]
84 #define msr_be  env->msr[MSR_BE]
85 #define msr_fe1 env->msr[MSR_FE1]
86 #define msr_ip  env->msr[MSR_IP]
87 #define msr_ir  env->msr[MSR_IR]
88 #define msr_dr  env->msr[MSR_DR]
89 #define msr_ri  env->msr[MSR_RI]
90 #define msr_le  env->msr[MSR_LE]
91
92 /* Segment registers */
93 typedef struct CPUPPCState {
94     /* general purpose registers */
95     uint32_t gpr[32];
96     /* floating point registers */
97     double fpr[32];
98     /* segment registers */
99     uint32_t sdr1;
100     uint32_t sr[16];
101     /* XER */
102     uint8_t xer[4];
103     /* Reservation address */
104     uint32_t reserve;
105     /* machine state register */
106     uint8_t msr[32];
107     /* condition register */
108     uint8_t crf[8];
109     /* floating point status and control register */
110     uint8_t fpscr[8];
111     uint32_t nip;
112     /* special purpose registers */
113     uint32_t lr;
114     uint32_t ctr;
115     /* BATs */
116     uint32_t DBAT[2][8];
117     uint32_t IBAT[2][8];
118     /* all others */
119     uint32_t spr[1024];
120     /* qemu dedicated */
121      /* temporary float registers */
122     double ft0;
123     double ft1;
124     double ft2;
125     int interrupt_request;
126     jmp_buf jmp_env;
127     int exception_index;
128     int error_code;
129     int access_type; /* when a memory exception occurs, the access
130                         type is stored here */
131     int user_mode_only; /* user mode only simulation */
132     struct TranslationBlock *current_tb; /* currently executing TB */
133     /* soft mmu support */
134     /* in order to avoid passing too many arguments to the memory
135        write helpers, we store some rarely used information in the CPU
136        context) */
137     unsigned long mem_write_pc; /* host pc at which the memory was
138                                    written */
139     unsigned long mem_write_vaddr; /* target virtual addr at which the
140                                       memory was written */
141     /* 0 = kernel, 1 = user (may have 2 = kernel code, 3 = user code ?) */
142     CPUTLBEntry tlb_read[2][CPU_TLB_SIZE];
143     CPUTLBEntry tlb_write[2][CPU_TLB_SIZE];
144
145     /* ice debug support */
146     uint32_t breakpoints[MAX_BREAKPOINTS];
147     int nb_breakpoints;
148     int singlestep_enabled; /* XXX: should use CPU single step mode instead */
149
150     /* Time base and decrementer */
151     ppc_tb_t *tb_env;
152
153     /* Power management */
154     int power_mode;
155
156     /* user data */
157     void *opaque;
158 } CPUPPCState;
159
160 CPUPPCState *cpu_ppc_init(void);
161 int cpu_ppc_exec(CPUPPCState *s);
162 void cpu_ppc_close(CPUPPCState *s);
163 /* you can call this signal handler from your SIGBUS and SIGSEGV
164    signal handlers to inform the virtual CPU of exceptions. non zero
165    is returned if the signal was handled by the virtual CPU.  */
166 struct siginfo;
167 int cpu_ppc_signal_handler(int host_signum, struct siginfo *info, 
168                            void *puc);
169
170 void do_interrupt (CPUPPCState *env);
171 void cpu_loop_exit(void);
172
173 void dump_stack (CPUPPCState *env);
174
175 uint32_t _load_xer (CPUPPCState *env);
176 void _store_xer (CPUPPCState *env, uint32_t value);
177 uint32_t _load_msr (CPUPPCState *env);
178 void _store_msr (CPUPPCState *env, uint32_t value);
179
180 int cpu_ppc_register (CPUPPCState *env, uint32_t pvr);
181
182 /* Time-base and decrementer management */
183 #ifndef NO_CPU_IO_DEFS
184 uint32_t cpu_ppc_load_tbl (CPUPPCState *env);
185 uint32_t cpu_ppc_load_tbu (CPUPPCState *env);
186 void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value);
187 void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value);
188 uint32_t cpu_ppc_load_decr (CPUPPCState *env);
189 void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value);
190 #endif
191
192 #define TARGET_PAGE_BITS 12
193 #include "cpu-all.h"
194
195 #define ugpr(n) (env->gpr[n])
196 #define fprd(n) (env->fpr[n])
197 #define fprs(n) ((float)env->fpr[n])
198 #define fpru(n) ((uint32_t)env->fpr[n])
199 #define fpri(n) ((int32_t)env->fpr[n])
200
201 #define SPR_ENCODE(sprn)                               \
202 (((sprn) >> 5) | (((sprn) & 0x1F) << 5))
203
204 /* User mode SPR */
205 #define spr(n) env->spr[n]
206 #define XER_SO 31
207 #define XER_OV 30
208 #define XER_CA 29
209 #define XER_BC 0
210 #define xer_so env->xer[3]
211 #define xer_ov env->xer[2]
212 #define xer_ca env->xer[1]
213 #define xer_bc env->xer[0]
214
215 #define MQ     SPR_ENCODE(0)
216 #define XER    SPR_ENCODE(1)
217 #define RTCUR  SPR_ENCODE(4)
218 #define RTCLR  SPR_ENCODE(5)
219 #define LR     SPR_ENCODE(8)
220 #define CTR    SPR_ENCODE(9)
221 /* VEA mode SPR */
222 #define V_TBL  SPR_ENCODE(268)
223 #define V_TBU  SPR_ENCODE(269)
224 /* supervisor mode SPR */
225 #define DSISR  SPR_ENCODE(18)
226 #define DAR    SPR_ENCODE(19)
227 #define RTCUW  SPR_ENCODE(20)
228 #define RTCLW  SPR_ENCODE(21)
229 #define DECR   SPR_ENCODE(22)
230 #define SDR1   SPR_ENCODE(25)
231 #define SRR0   SPR_ENCODE(26)
232 #define SRR1   SPR_ENCODE(27)
233 #define SPRG0  SPR_ENCODE(272)
234 #define SPRG1  SPR_ENCODE(273)
235 #define SPRG2  SPR_ENCODE(274)
236 #define SPRG3  SPR_ENCODE(275)
237 #define SPRG4  SPR_ENCODE(276)
238 #define SPRG5  SPR_ENCODE(277)
239 #define SPRG6  SPR_ENCODE(278)
240 #define SPRG7  SPR_ENCODE(279)
241 #define ASR    SPR_ENCODE(280)
242 #define EAR    SPR_ENCODE(282)
243 #define O_TBL  SPR_ENCODE(284)
244 #define O_TBU  SPR_ENCODE(285)
245 #define PVR    SPR_ENCODE(287)
246 #define IBAT0U SPR_ENCODE(528)
247 #define IBAT0L SPR_ENCODE(529)
248 #define IBAT1U SPR_ENCODE(530)
249 #define IBAT1L SPR_ENCODE(531)
250 #define IBAT2U SPR_ENCODE(532)
251 #define IBAT2L SPR_ENCODE(533)
252 #define IBAT3U SPR_ENCODE(534)
253 #define IBAT3L SPR_ENCODE(535)
254 #define DBAT0U SPR_ENCODE(536)
255 #define DBAT0L SPR_ENCODE(537)
256 #define DBAT1U SPR_ENCODE(538)
257 #define DBAT1L SPR_ENCODE(539)
258 #define DBAT2U SPR_ENCODE(540)
259 #define DBAT2L SPR_ENCODE(541)
260 #define DBAT3U SPR_ENCODE(542)
261 #define DBAT3L SPR_ENCODE(543)
262 #define IBAT4U SPR_ENCODE(560)
263 #define IBAT4L SPR_ENCODE(561)
264 #define IBAT5U SPR_ENCODE(562)
265 #define IBAT5L SPR_ENCODE(563)
266 #define IBAT6U SPR_ENCODE(564)
267 #define IBAT6L SPR_ENCODE(565)
268 #define IBAT7U SPR_ENCODE(566)
269 #define IBAT7L SPR_ENCODE(567)
270 #define DBAT4U SPR_ENCODE(568)
271 #define DBAT4L SPR_ENCODE(569)
272 #define DBAT5U SPR_ENCODE(570)
273 #define DBAT5L SPR_ENCODE(571)
274 #define DBAT6U SPR_ENCODE(572)
275 #define DBAT6L SPR_ENCODE(573)
276 #define DBAT7U SPR_ENCODE(574)
277 #define DBAT7L SPR_ENCODE(575)
278 #define UMMCR0 SPR_ENCODE(936)
279 #define UPMC1  SPR_ENCODE(937)
280 #define UPMC2  SPR_ENCODE(938)
281 #define USIA   SPR_ENCODE(939)
282 #define UMMCR1 SPR_ENCODE(940)
283 #define UPMC3  SPR_ENCODE(941)
284 #define UPMC4  SPR_ENCODE(942)
285 #define MMCR0  SPR_ENCODE(952)
286 #define PMC1   SPR_ENCODE(953)
287 #define PMC2   SPR_ENCODE(954)
288 #define SIA    SPR_ENCODE(955)
289 #define MMCR1  SPR_ENCODE(956)
290 #define PMC3   SPR_ENCODE(957)
291 #define PMC4   SPR_ENCODE(958)
292 #define SDA    SPR_ENCODE(959)
293 #define DMISS  SPR_ENCODE(976)
294 #define DCMP   SPR_ENCODE(977)
295 #define DHASH1 SPR_ENCODE(978)
296 #define DHASH2 SPR_ENCODE(979)
297 #define IMISS  SPR_ENCODE(980)
298 #define ICMP   SPR_ENCODE(981)
299 #define RPA    SPR_ENCODE(982)
300 #define TCR    SPR_ENCODE(984)
301 #define IBR    SPR_ENCODE(986)
302 #define ESASRR SPR_ENCODE(987)
303 #define SEBR   SPR_ENCODE(990)
304 #define SER    SPR_ENCODE(991)
305 #define HID0   SPR_ENCODE(1008)
306 #define HID1   SPR_ENCODE(1009)
307 #define IABR   SPR_ENCODE(1010)
308 #define HID2   SPR_ENCODE(1011)
309 #define DABR   SPR_ENCODE(1013)
310 #define L2PM   SPR_ENCODE(1016)
311 #define L2CR   SPR_ENCODE(1017)
312 #define ICTC   SPR_ENCODE(1019)
313 #define THRM1  SPR_ENCODE(1020)
314 #define THRM2  SPR_ENCODE(1021)
315 #define THRM3  SPR_ENCODE(1022)
316 #define SP     SPR_ENCODE(1021)
317 #define SPR_LP SPR_ENCODE(1022)
318 #define DABR_MASK 0xFFFFFFF8
319 #define FPECR  SPR_ENCODE(1022)
320 #define PIR    SPR_ENCODE(1023)
321
322 /* Memory access type :
323  * may be needed for precise access rights control and precise exceptions.
324  */
325 enum {
326     /* 1 bit to define user level / supervisor access */
327     ACCESS_USER  = 0x00,
328     ACCESS_SUPER = 0x01,
329     /* Type of instruction that generated the access */
330     ACCESS_CODE  = 0x10, /* Code fetch access                */
331     ACCESS_INT   = 0x20, /* Integer load/store access        */
332     ACCESS_FLOAT = 0x30, /* floating point load/store access */
333     ACCESS_RES   = 0x40, /* load/store with reservation      */
334     ACCESS_EXT   = 0x50, /* external access                  */
335     ACCESS_CACHE = 0x60, /* Cache manipulation               */
336 };
337
338 /*****************************************************************************/
339 /* Exceptions */
340 enum {
341     EXCP_NONE          = -1,
342     /* PPC hardware exceptions : exception vector / 0x100 */
343     EXCP_RESET         = 0x01, /* System reset                     */
344     EXCP_MACHINE_CHECK = 0x02, /* Machine check exception          */
345     EXCP_DSI           = 0x03, /* Impossible memory access         */
346     EXCP_ISI           = 0x04, /* Impossible instruction fetch     */
347     EXCP_EXTERNAL      = 0x05, /* External interruption            */
348     EXCP_ALIGN         = 0x06, /* Alignment exception              */
349     EXCP_PROGRAM       = 0x07, /* Program exception                */
350     EXCP_NO_FP         = 0x08, /* No floating point                */
351     EXCP_DECR          = 0x09, /* Decrementer exception            */
352     EXCP_RESA          = 0x0A, /* Implementation specific          */
353     EXCP_RESB          = 0x0B, /* Implementation specific          */
354     EXCP_SYSCALL       = 0x0C, /* System call                      */
355     EXCP_TRACE         = 0x0D, /* Trace exception (optional)       */
356     EXCP_FP_ASSIST     = 0x0E, /* Floating-point assist (optional) */
357     /* MPC740/745/750 & IBM 750 */
358     EXCP_PERF          = 0x0F,  /* Performance monitor              */
359     EXCP_IABR          = 0x13,  /* Instruction address breakpoint   */
360     EXCP_SMI           = 0x14,  /* System management interrupt      */
361     EXCP_THRM          = 0x15,  /* Thermal management interrupt     */
362     /* MPC755 */
363     EXCP_TLBMISS       = 0x10,  /* Instruction TLB miss             */
364     EXCP_TLBMISS_DL    = 0x11,  /* Data TLB miss for load           */
365     EXCP_TLBMISS_DS    = 0x12,  /* Data TLB miss for store          */
366     EXCP_PPC_MAX       = 0x16,
367     /* Qemu exception */
368     EXCP_OFCALL        = 0x20,  /* Call open-firmware emulator      */
369     EXCP_RTASCALL      = 0x21,  /* Call RTAS emulator               */
370     /* Special cases where we want to stop translation */
371     EXCP_MTMSR         = 0x104, /* mtmsr instruction:               */
372                                 /* may change privilege level       */
373     EXCP_BRANCH        = 0x108, /* branch instruction               */
374     EXCP_RFI           = 0x10C, /* return from interrupt            */
375     EXCP_SYSCALL_USER  = 0x110, /* System call in user mode only    */
376 };
377 /* Error codes */
378 enum {
379     /* Exception subtypes for EXCP_DSI                              */
380     EXCP_DSI_TRANSLATE = 0x01,  /* Data address can't be translated */
381     EXCP_DSI_NOTSUP    = 0x02,  /* Access type not supported        */
382     EXCP_DSI_PROT      = 0x03,  /* Memory protection violation      */
383     EXCP_DSI_EXTERNAL  = 0x04,  /* External access disabled         */
384     EXCP_DSI_DABR      = 0x05,  /* Data address breakpoint          */
385     /* flags for EXCP_DSI */
386     EXCP_DSI_DIRECT    = 0x10,
387     EXCP_DSI_STORE     = 0x20,
388     EXCP_DSI_ECXW      = 0x40,
389     /* Exception subtypes for EXCP_ISI                              */
390     EXCP_ISI_TRANSLATE = 0x01,  /* Code address can't be translated */
391     EXCP_ISI_NOEXEC    = 0x02,  /* Try to fetch from a data segment */
392     EXCP_ISI_GUARD     = 0x03,  /* Fetch from guarded memory        */
393     EXCP_ISI_PROT      = 0x04,  /* Memory protection violation      */
394     EXCP_ISI_DIRECT    = 0x05,  /* Trying to fetch from             *
395                                  * a direct store segment           */
396     /* Exception subtypes for EXCP_ALIGN                            */
397     EXCP_ALIGN_FP      = 0x01,  /* FP alignment exception           */
398     EXCP_ALIGN_LST     = 0x02,  /* Unaligned mult/extern load/store */
399     EXCP_ALIGN_LE      = 0x03,  /* Multiple little-endian access    */
400     EXCP_ALIGN_PROT    = 0x04,  /* Access cross protection boundary */
401     EXCP_ALIGN_BAT     = 0x05,  /* Access cross a BAT/seg boundary  */
402     EXCP_ALIGN_CACHE   = 0x06,  /* Impossible dcbz access           */
403     /* Exception subtypes for EXCP_PROGRAM                          */
404     /* FP exceptions */
405     EXCP_FP            = 0x10,
406     EXCP_FP_OX         = 0x01,  /* FP overflow                      */
407     EXCP_FP_UX         = 0x02,  /* FP underflow                     */
408     EXCP_FP_ZX         = 0x03,  /* FP divide by zero                */
409     EXCP_FP_XX         = 0x04,  /* FP inexact                       */
410     EXCP_FP_VXNAN      = 0x05,  /* FP invalid SNaN op               */
411     EXCP_FP_VXISI      = 0x06,  /* FP invalid infinite substraction */
412     EXCP_FP_VXIDI      = 0x07,  /* FP invalid infinite divide       */
413     EXCP_FP_VXZDZ      = 0x08,  /* FP invalid zero divide           */
414     EXCP_FP_VXIMZ      = 0x09,  /* FP invalid infinite * zero       */
415     EXCP_FP_VXVC       = 0x0A,  /* FP invalid compare               */
416     EXCP_FP_VXSOFT     = 0x0B,  /* FP invalid operation             */
417     EXCP_FP_VXSQRT     = 0x0C,  /* FP invalid square root           */
418     EXCP_FP_VXCVI      = 0x0D,  /* FP invalid integer conversion    */
419     /* Invalid instruction */
420     EXCP_INVAL         = 0x20,
421     EXCP_INVAL_INVAL   = 0x01,  /* Invalid instruction              */
422     EXCP_INVAL_LSWX    = 0x02,  /* Invalid lswx instruction         */
423     EXCP_INVAL_SPR     = 0x03,  /* Invalid SPR access               */
424     EXCP_INVAL_FP      = 0x04,  /* Unimplemented mandatory fp instr */
425     /* Privileged instruction */
426     EXCP_PRIV          = 0x30,
427     EXCP_PRIV_OPC      = 0x01,
428     EXCP_PRIV_REG      = 0x02,
429     /* Trap */
430     EXCP_TRAP          = 0x40,
431 };
432
433 /*****************************************************************************/
434
435 #endif /* !defined (__CPU_PPC_H__) */