xref: /qemu/target/ppc/internal.h (revision 1db8af5c87ef5c89ecdb9e2d2620cd38cfbca940)
13e00884fSGautham R. Shenoy /*
2136fbf65Szhaolichang  *  PowerPC internal definitions for qemu.
33e00884fSGautham R. Shenoy  *
43e00884fSGautham R. Shenoy  * This library is free software; you can redistribute it and/or
53e00884fSGautham R. Shenoy  * modify it under the terms of the GNU Lesser General Public
63e00884fSGautham R. Shenoy  * License as published by the Free Software Foundation; either
76bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
83e00884fSGautham R. Shenoy  *
93e00884fSGautham R. Shenoy  * This library is distributed in the hope that it will be useful,
103e00884fSGautham R. Shenoy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
113e00884fSGautham R. Shenoy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
123e00884fSGautham R. Shenoy  * Lesser General Public License for more details.
133e00884fSGautham R. Shenoy  *
143e00884fSGautham R. Shenoy  * You should have received a copy of the GNU Lesser General Public
153e00884fSGautham R. Shenoy  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
163e00884fSGautham R. Shenoy  */
173e00884fSGautham R. Shenoy 
183e00884fSGautham R. Shenoy #ifndef PPC_INTERNAL_H
193e00884fSGautham R. Shenoy #define PPC_INTERNAL_H
203e00884fSGautham R. Shenoy 
213e00884fSGautham R. Shenoy #define FUNC_MASK(name, ret_type, size, max_val)                  \
223e00884fSGautham R. Shenoy static inline ret_type name(uint##size##_t start,                 \
233e00884fSGautham R. Shenoy                               uint##size##_t end)                 \
243e00884fSGautham R. Shenoy {                                                                 \
253e00884fSGautham R. Shenoy     ret_type ret, max_bit = size - 1;                             \
263e00884fSGautham R. Shenoy                                                                   \
273e00884fSGautham R. Shenoy     if (likely(start == 0)) {                                     \
283e00884fSGautham R. Shenoy         ret = max_val << (max_bit - end);                         \
293e00884fSGautham R. Shenoy     } else if (likely(end == max_bit)) {                          \
303e00884fSGautham R. Shenoy         ret = max_val >> start;                                   \
313e00884fSGautham R. Shenoy     } else {                                                      \
323e00884fSGautham R. Shenoy         ret = (((uint##size##_t)(-1ULL)) >> (start)) ^            \
333e00884fSGautham R. Shenoy             (((uint##size##_t)(-1ULL) >> (end)) >> 1);            \
343e00884fSGautham R. Shenoy         if (unlikely(start > end)) {                              \
353e00884fSGautham R. Shenoy             return ~ret;                                          \
363e00884fSGautham R. Shenoy         }                                                         \
373e00884fSGautham R. Shenoy     }                                                             \
383e00884fSGautham R. Shenoy                                                                   \
393e00884fSGautham R. Shenoy     return ret;                                                   \
403e00884fSGautham R. Shenoy }
413e00884fSGautham R. Shenoy 
423e00884fSGautham R. Shenoy #if defined(TARGET_PPC64)
433e00884fSGautham R. Shenoy FUNC_MASK(MASK, target_ulong, 64, UINT64_MAX);
443e00884fSGautham R. Shenoy #else
453e00884fSGautham R. Shenoy FUNC_MASK(MASK, target_ulong, 32, UINT32_MAX);
463e00884fSGautham R. Shenoy #endif
473e00884fSGautham R. Shenoy FUNC_MASK(mask_u32, uint32_t, 32, UINT32_MAX);
483e00884fSGautham R. Shenoy FUNC_MASK(mask_u64, uint64_t, 64, UINT64_MAX);
493e00884fSGautham R. Shenoy 
50985e3023SBharata B Rao /*****************************************************************************/
51985e3023SBharata B Rao /***                           Instruction decoding                        ***/
52985e3023SBharata B Rao #define EXTRACT_HELPER(name, shift, nb)                                       \
53985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode)                                  \
54985e3023SBharata B Rao {                                                                             \
554c23c2a5SMark Cave-Ayland     return extract32(opcode, shift, nb);                                      \
56985e3023SBharata B Rao }
57985e3023SBharata B Rao 
58985e3023SBharata B Rao #define EXTRACT_SHELPER(name, shift, nb)                                      \
59985e3023SBharata B Rao static inline int32_t name(uint32_t opcode)                                   \
60985e3023SBharata B Rao {                                                                             \
614c23c2a5SMark Cave-Ayland     return sextract32(opcode, shift, nb);                                     \
62985e3023SBharata B Rao }
63985e3023SBharata B Rao 
64985e3023SBharata B Rao #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2)                  \
65985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode)                                  \
66985e3023SBharata B Rao {                                                                             \
674c23c2a5SMark Cave-Ayland     return extract32(opcode, shift1, nb1) << nb2 |                            \
684c23c2a5SMark Cave-Ayland                extract32(opcode, shift2, nb2);                                \
69985e3023SBharata B Rao }
70985e3023SBharata B Rao 
71403a884aSNikunj A Dadhania #define EXTRACT_HELPER_SPLIT_3(name,                                          \
72985e3023SBharata B Rao                               d0_bits, shift_op_d0, shift_d0,                 \
73985e3023SBharata B Rao                               d1_bits, shift_op_d1, shift_d1,                 \
74985e3023SBharata B Rao                               d2_bits, shift_op_d2, shift_d2)                 \
75985e3023SBharata B Rao static inline int16_t name(uint32_t opcode)                                   \
76985e3023SBharata B Rao {                                                                             \
77985e3023SBharata B Rao     return                                                                    \
78985e3023SBharata B Rao         (((opcode >> (shift_op_d0)) & ((1 << (d0_bits)) - 1)) << (shift_d0)) | \
79985e3023SBharata B Rao         (((opcode >> (shift_op_d1)) & ((1 << (d1_bits)) - 1)) << (shift_d1)) | \
80985e3023SBharata B Rao         (((opcode >> (shift_op_d2)) & ((1 << (d2_bits)) - 1)) << (shift_d2));  \
81985e3023SBharata B Rao }
82985e3023SBharata B Rao 
83985e3023SBharata B Rao 
84985e3023SBharata B Rao /* Opcode part 1 */
85985e3023SBharata B Rao EXTRACT_HELPER(opc1, 26, 6);
86985e3023SBharata B Rao /* Opcode part 2 */
87985e3023SBharata B Rao EXTRACT_HELPER(opc2, 1, 5);
88985e3023SBharata B Rao /* Opcode part 3 */
89985e3023SBharata B Rao EXTRACT_HELPER(opc3, 6, 5);
90985e3023SBharata B Rao /* Opcode part 4 */
91985e3023SBharata B Rao EXTRACT_HELPER(opc4, 16, 5);
92985e3023SBharata B Rao /* Update Cr0 flags */
93985e3023SBharata B Rao EXTRACT_HELPER(Rc, 0, 1);
94985e3023SBharata B Rao /* Update Cr6 flags (Altivec) */
95985e3023SBharata B Rao EXTRACT_HELPER(Rc21, 10, 1);
96985e3023SBharata B Rao /* Destination */
97985e3023SBharata B Rao EXTRACT_HELPER(rD, 21, 5);
98985e3023SBharata B Rao /* Source */
99985e3023SBharata B Rao EXTRACT_HELPER(rS, 21, 5);
100985e3023SBharata B Rao /* First operand */
101985e3023SBharata B Rao EXTRACT_HELPER(rA, 16, 5);
102985e3023SBharata B Rao /* Second operand */
103985e3023SBharata B Rao EXTRACT_HELPER(rB, 11, 5);
104985e3023SBharata B Rao /* Third operand */
105985e3023SBharata B Rao EXTRACT_HELPER(rC, 6, 5);
106985e3023SBharata B Rao /***                               Get CRn                                 ***/
107985e3023SBharata B Rao EXTRACT_HELPER(crfD, 23, 3);
108985e3023SBharata B Rao EXTRACT_HELPER(BF, 23, 3);
109985e3023SBharata B Rao EXTRACT_HELPER(crfS, 18, 3);
110985e3023SBharata B Rao EXTRACT_HELPER(crbD, 21, 5);
111985e3023SBharata B Rao EXTRACT_HELPER(crbA, 16, 5);
112985e3023SBharata B Rao EXTRACT_HELPER(crbB, 11, 5);
113985e3023SBharata B Rao /* SPR / TBL */
114985e3023SBharata B Rao EXTRACT_HELPER(_SPR, 11, 10);
115985e3023SBharata B Rao static inline uint32_t SPR(uint32_t opcode)
116985e3023SBharata B Rao {
117985e3023SBharata B Rao     uint32_t sprn = _SPR(opcode);
118985e3023SBharata B Rao 
119985e3023SBharata B Rao     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
120985e3023SBharata B Rao }
121985e3023SBharata B Rao /***                              Get constants                            ***/
122985e3023SBharata B Rao /* 16 bits signed immediate value */
123985e3023SBharata B Rao EXTRACT_SHELPER(SIMM, 0, 16);
124985e3023SBharata B Rao /* 16 bits unsigned immediate value */
125985e3023SBharata B Rao EXTRACT_HELPER(UIMM, 0, 16);
126985e3023SBharata B Rao /* 5 bits signed immediate value */
127ffcd21acSMark Cave-Ayland EXTRACT_SHELPER(SIMM5, 16, 5);
128985e3023SBharata B Rao /* 5 bits signed immediate value */
129985e3023SBharata B Rao EXTRACT_HELPER(UIMM5, 16, 5);
130985e3023SBharata B Rao /* 4 bits unsigned immediate value */
131985e3023SBharata B Rao EXTRACT_HELPER(UIMM4, 16, 4);
132985e3023SBharata B Rao /* Bit count */
133985e3023SBharata B Rao EXTRACT_HELPER(NB, 11, 5);
134985e3023SBharata B Rao /* Shift count */
135985e3023SBharata B Rao EXTRACT_HELPER(SH, 11, 5);
136a68a6146SBalamuruhan S /* lwat/stwat/ldat/lwat */
137a68a6146SBalamuruhan S EXTRACT_HELPER(FC, 11, 5);
138985e3023SBharata B Rao /* Vector shift count */
139985e3023SBharata B Rao EXTRACT_HELPER(VSH, 6, 4);
140985e3023SBharata B Rao /* Mask start */
141985e3023SBharata B Rao EXTRACT_HELPER(MB, 6, 5);
142985e3023SBharata B Rao /* Mask end */
143985e3023SBharata B Rao EXTRACT_HELPER(ME, 1, 5);
144985e3023SBharata B Rao /* Trap operand */
145985e3023SBharata B Rao EXTRACT_HELPER(TO, 21, 5);
146985e3023SBharata B Rao 
147985e3023SBharata B Rao EXTRACT_HELPER(CRM, 12, 8);
148985e3023SBharata B Rao 
149985e3023SBharata B Rao #ifndef CONFIG_USER_ONLY
150985e3023SBharata B Rao EXTRACT_HELPER(SR, 16, 4);
151985e3023SBharata B Rao #endif
152985e3023SBharata B Rao 
153985e3023SBharata B Rao /* mtfsf/mtfsfi */
154985e3023SBharata B Rao EXTRACT_HELPER(FPBF, 23, 3);
155985e3023SBharata B Rao EXTRACT_HELPER(FPIMM, 12, 4);
156985e3023SBharata B Rao EXTRACT_HELPER(FPL, 25, 1);
157985e3023SBharata B Rao EXTRACT_HELPER(FPFLM, 17, 8);
158985e3023SBharata B Rao EXTRACT_HELPER(FPW, 16, 1);
159985e3023SBharata B Rao 
160a2735cf4SPaul A. Clarke /* mffscrni */
161a2735cf4SPaul A. Clarke EXTRACT_HELPER(RM, 11, 2);
162a2735cf4SPaul A. Clarke 
163985e3023SBharata B Rao /* addpcis */
164403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0)
165985e3023SBharata B Rao #if defined(TARGET_PPC64)
166985e3023SBharata B Rao /* darn */
167985e3023SBharata B Rao EXTRACT_HELPER(L, 16, 2);
168985e3023SBharata B Rao #endif
169985e3023SBharata B Rao 
170985e3023SBharata B Rao /***                            Jump target decoding                       ***/
171985e3023SBharata B Rao /* Immediate address */
172985e3023SBharata B Rao static inline target_ulong LI(uint32_t opcode)
173985e3023SBharata B Rao {
174985e3023SBharata B Rao     return (opcode >> 0) & 0x03FFFFFC;
175985e3023SBharata B Rao }
176985e3023SBharata B Rao 
177985e3023SBharata B Rao static inline uint32_t BD(uint32_t opcode)
178985e3023SBharata B Rao {
179985e3023SBharata B Rao     return (opcode >> 0) & 0xFFFC;
180985e3023SBharata B Rao }
181985e3023SBharata B Rao 
182985e3023SBharata B Rao EXTRACT_HELPER(BO, 21, 5);
183985e3023SBharata B Rao EXTRACT_HELPER(BI, 16, 5);
184985e3023SBharata B Rao /* Absolute/relative address */
185985e3023SBharata B Rao EXTRACT_HELPER(AA, 1, 1);
186985e3023SBharata B Rao /* Link */
187985e3023SBharata B Rao EXTRACT_HELPER(LK, 0, 1);
188985e3023SBharata B Rao 
189985e3023SBharata B Rao /* DFP Z22-form */
190985e3023SBharata B Rao EXTRACT_HELPER(DCM, 10, 6)
191985e3023SBharata B Rao 
192985e3023SBharata B Rao /* DFP Z23-form */
193985e3023SBharata B Rao EXTRACT_HELPER(RMC, 9, 2)
194be07ad58SJose Ricardo Ziviani EXTRACT_HELPER(Rrm, 16, 1)
195985e3023SBharata B Rao 
196d59ba583SNikunj A Dadhania EXTRACT_HELPER_SPLIT(DQxT, 3, 1, 21, 5);
197985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
198985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
199985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
200985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
201985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
202985e3023SBharata B Rao EXTRACT_HELPER(DM, 8, 2);
203985e3023SBharata B Rao EXTRACT_HELPER(UIM, 16, 2);
204985e3023SBharata B Rao EXTRACT_HELPER(SHW, 8, 2);
205985e3023SBharata B Rao EXTRACT_HELPER(SP, 19, 2);
206985e3023SBharata B Rao EXTRACT_HELPER(IMM8, 11, 8);
20778241762SNikunj A Dadhania EXTRACT_HELPER(DCMX, 16, 7);
208403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DCMX_XV, 5, 16, 0, 1, 2, 5, 1, 6, 6);
209985e3023SBharata B Rao 
210f566c047SBharata B Rao void helper_compute_fprf_float16(CPUPPCState *env, float16 arg);
2119aeae8e1SBharata B Rao void helper_compute_fprf_float32(CPUPPCState *env, float32 arg);
21207bdd247SBharata B Rao void helper_compute_fprf_float128(CPUPPCState *env, float128 arg);
2130f3110faSRichard Henderson 
2140f3110faSRichard Henderson /* Raise a data fault alignment exception for the specified virtual address */
2150f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
216fa947a66SRichard Henderson                                  MMUAccessType access_type, int mmu_idx,
217fa947a66SRichard Henderson                                  uintptr_t retaddr) QEMU_NORETURN;
21835a5d74eSBruno Larsen (billionai) 
2197468e2c8SBruno Larsen (billionai) /* translate.c */
2207468e2c8SBruno Larsen (billionai) 
2217468e2c8SBruno Larsen (billionai) int ppc_fixup_cpu(PowerPCCPU *cpu);
2227468e2c8SBruno Larsen (billionai) void create_ppc_opcodes(PowerPCCPU *cpu, Error **errp);
2237468e2c8SBruno Larsen (billionai) void destroy_ppc_opcodes(PowerPCCPU *cpu);
2247468e2c8SBruno Larsen (billionai) 
22535a5d74eSBruno Larsen (billionai) /* gdbstub.c */
22635a5d74eSBruno Larsen (billionai) void ppc_gdb_init(CPUState *cs, PowerPCCPUClass *ppc);
22735a5d74eSBruno Larsen (billionai) gchar *ppc_gdb_arch_name(CPUState *cs);
22835a5d74eSBruno Larsen (billionai) 
229182357dbSRichard Henderson /**
230182357dbSRichard Henderson  * prot_for_access_type:
231182357dbSRichard Henderson  * @access_type: Access type
232182357dbSRichard Henderson  *
233182357dbSRichard Henderson  * Return the protection bit required for the given access type.
234182357dbSRichard Henderson  */
235182357dbSRichard Henderson static inline int prot_for_access_type(MMUAccessType access_type)
236182357dbSRichard Henderson {
237182357dbSRichard Henderson     switch (access_type) {
238182357dbSRichard Henderson     case MMU_INST_FETCH:
239182357dbSRichard Henderson         return PAGE_EXEC;
240182357dbSRichard Henderson     case MMU_DATA_LOAD:
241182357dbSRichard Henderson         return PAGE_READ;
242182357dbSRichard Henderson     case MMU_DATA_STORE:
243182357dbSRichard Henderson         return PAGE_WRITE;
244182357dbSRichard Henderson     }
245182357dbSRichard Henderson     g_assert_not_reached();
246182357dbSRichard Henderson }
247182357dbSRichard Henderson 
2485118ebe8SLucas Mateus Castro (alqotel) /* PowerPC MMU emulation */
2495118ebe8SLucas Mateus Castro (alqotel) 
2505118ebe8SLucas Mateus Castro (alqotel) typedef struct mmu_ctx_t mmu_ctx_t;
2515118ebe8SLucas Mateus Castro (alqotel) bool ppc_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
2525118ebe8SLucas Mateus Castro (alqotel)                       hwaddr *raddrp, int *psizep, int *protp,
2535118ebe8SLucas Mateus Castro (alqotel)                       int mmu_idx, bool guest_visible);
2545118ebe8SLucas Mateus Castro (alqotel) int get_physical_address_wtlb(CPUPPCState *env, mmu_ctx_t *ctx,
2555118ebe8SLucas Mateus Castro (alqotel)                                      target_ulong eaddr,
2565118ebe8SLucas Mateus Castro (alqotel)                                      MMUAccessType access_type, int type,
2575118ebe8SLucas Mateus Castro (alqotel)                                      int mmu_idx);
2585118ebe8SLucas Mateus Castro (alqotel) /* Software driven TLB helpers */
2595118ebe8SLucas Mateus Castro (alqotel) int ppc6xx_tlb_getnum(CPUPPCState *env, target_ulong eaddr,
2605118ebe8SLucas Mateus Castro (alqotel)                                     int way, int is_code);
2615118ebe8SLucas Mateus Castro (alqotel) /* Context used internally during MMU translations */
2625118ebe8SLucas Mateus Castro (alqotel) struct mmu_ctx_t {
2635118ebe8SLucas Mateus Castro (alqotel)     hwaddr raddr;      /* Real address              */
2645118ebe8SLucas Mateus Castro (alqotel)     hwaddr eaddr;      /* Effective address         */
2655118ebe8SLucas Mateus Castro (alqotel)     int prot;                      /* Protection bits           */
2665118ebe8SLucas Mateus Castro (alqotel)     hwaddr hash[2];    /* Pagetable hash values     */
2675118ebe8SLucas Mateus Castro (alqotel)     target_ulong ptem;             /* Virtual segment ID | API  */
2685118ebe8SLucas Mateus Castro (alqotel)     int key;                       /* Access key                */
2695118ebe8SLucas Mateus Castro (alqotel)     int nx;                        /* Non-execute area          */
2705118ebe8SLucas Mateus Castro (alqotel) };
2715118ebe8SLucas Mateus Castro (alqotel) 
2725118ebe8SLucas Mateus Castro (alqotel) /* Common routines used by software and hardware TLBs emulation */
2735118ebe8SLucas Mateus Castro (alqotel) static inline int pte_is_valid(target_ulong pte0)
2745118ebe8SLucas Mateus Castro (alqotel) {
2755118ebe8SLucas Mateus Castro (alqotel)     return pte0 & 0x80000000 ? 1 : 0;
2765118ebe8SLucas Mateus Castro (alqotel) }
2775118ebe8SLucas Mateus Castro (alqotel) 
2785118ebe8SLucas Mateus Castro (alqotel) static inline void pte_invalidate(target_ulong *pte0)
2795118ebe8SLucas Mateus Castro (alqotel) {
2805118ebe8SLucas Mateus Castro (alqotel)     *pte0 &= ~0x80000000;
2815118ebe8SLucas Mateus Castro (alqotel) }
2825118ebe8SLucas Mateus Castro (alqotel) 
2835118ebe8SLucas Mateus Castro (alqotel) #define PTE_PTEM_MASK 0x7FFFFFBF
2845118ebe8SLucas Mateus Castro (alqotel) #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
2855118ebe8SLucas Mateus Castro (alqotel) 
286*1db8af5cSRichard Henderson #ifdef CONFIG_USER_ONLY
287*1db8af5cSRichard Henderson void ppc_cpu_record_sigsegv(CPUState *cs, vaddr addr,
288*1db8af5cSRichard Henderson                             MMUAccessType access_type,
289*1db8af5cSRichard Henderson                             bool maperr, uintptr_t ra);
290*1db8af5cSRichard Henderson #else
291*1db8af5cSRichard Henderson bool ppc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
292*1db8af5cSRichard Henderson                       MMUAccessType access_type, int mmu_idx,
293*1db8af5cSRichard Henderson                       bool probe, uintptr_t retaddr);
294*1db8af5cSRichard Henderson #endif
2955118ebe8SLucas Mateus Castro (alqotel) 
2963e00884fSGautham R. Shenoy #endif /* PPC_INTERNAL_H */
297