xref: /qemu/target/hppa/cpu.c (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
1 /*
2  * QEMU HPPA CPU
3  *
4  * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
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.1 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, see
18  * <http://www.gnu.org/licenses/lgpl-2.1.html>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/qemu-print.h"
24 #include "qemu/timer.h"
25 #include "cpu.h"
26 #include "qemu/module.h"
27 #include "exec/exec-all.h"
28 #include "exec/translation-block.h"
29 #include "fpu/softfloat.h"
30 #include "tcg/tcg.h"
31 #include "hw/hppa/hppa_hardware.h"
32 
33 static void hppa_cpu_set_pc(CPUState *cs, vaddr value)
34 {
35     HPPACPU *cpu = HPPA_CPU(cs);
36 
37 #ifdef CONFIG_USER_ONLY
38     value |= PRIV_USER;
39 #endif
40     cpu->env.iaoq_f = value;
41     cpu->env.iaoq_b = value + 4;
42 }
43 
44 static vaddr hppa_cpu_get_pc(CPUState *cs)
45 {
46     CPUHPPAState *env = cpu_env(cs);
47 
48     return hppa_form_gva_mask(env->gva_offset_mask,
49                          (env->psw & PSW_C ? env->iasq_f : 0),
50                          env->iaoq_f & -4);
51 }
52 
53 void cpu_get_tb_cpu_state(CPUHPPAState *env, vaddr *pc,
54                           uint64_t *pcsbase, uint32_t *pflags)
55 {
56     uint32_t flags = 0;
57     uint64_t cs_base = 0;
58 
59     /*
60      * TB lookup assumes that PC contains the complete virtual address.
61      * If we leave space+offset separate, we'll get ITLB misses to an
62      * incomplete virtual address.  This also means that we must separate
63      * out current cpu privilege from the low bits of IAOQ_F.
64      */
65     *pc = hppa_cpu_get_pc(env_cpu(env));
66     flags |= (env->iaoq_f & 3) << TB_FLAG_PRIV_SHIFT;
67 
68     /*
69      * The only really interesting case is if IAQ_Back is on the same page
70      * as IAQ_Front, so that we can use goto_tb between the blocks.  In all
71      * other cases, we'll be ending the TranslationBlock with one insn and
72      * not linking between them.
73      */
74     if (env->iasq_f != env->iasq_b) {
75         cs_base |= CS_BASE_DIFFSPACE;
76     } else if ((env->iaoq_f ^ env->iaoq_b) & TARGET_PAGE_MASK) {
77         cs_base |= CS_BASE_DIFFPAGE;
78     } else {
79         cs_base |= env->iaoq_b & ~TARGET_PAGE_MASK;
80     }
81 
82     /* ??? E, T, H, L bits need to be here, when implemented.  */
83     flags |= env->psw_n * PSW_N;
84     flags |= env->psw_xb;
85     flags |= env->psw & (PSW_W | PSW_C | PSW_D | PSW_P);
86 
87 #ifdef CONFIG_USER_ONLY
88     flags |= TB_FLAG_UNALIGN * !env_cpu(env)->prctl_unalign_sigbus;
89 #else
90     if ((env->sr[4] == env->sr[5])
91         & (env->sr[4] == env->sr[6])
92         & (env->sr[4] == env->sr[7])) {
93         flags |= TB_FLAG_SR_SAME;
94     }
95     if ((env->psw & PSW_W) &&
96         (env->dr[2] & HPPA64_DIAG_SPHASH_ENABLE)) {
97         flags |= TB_FLAG_SPHASH;
98     }
99 #endif
100 
101     *pcsbase = cs_base;
102     *pflags = flags;
103 }
104 
105 static void hppa_cpu_synchronize_from_tb(CPUState *cs,
106                                          const TranslationBlock *tb)
107 {
108     HPPACPU *cpu = HPPA_CPU(cs);
109 
110     /* IAQ is always up-to-date before goto_tb. */
111     cpu->env.psw_n = (tb->flags & PSW_N) != 0;
112     cpu->env.psw_xb = tb->flags & (PSW_X | PSW_B);
113 }
114 
115 static void hppa_restore_state_to_opc(CPUState *cs,
116                                       const TranslationBlock *tb,
117                                       const uint64_t *data)
118 {
119     CPUHPPAState *env = cpu_env(cs);
120 
121     env->iaoq_f = (env->iaoq_f & TARGET_PAGE_MASK) | data[0];
122     if (data[1] != INT32_MIN) {
123         env->iaoq_b = env->iaoq_f + data[1];
124     }
125     env->unwind_breg = data[2];
126     /*
127      * Since we were executing the instruction at IAOQ_F, and took some
128      * sort of action that provoked the cpu_restore_state, we can infer
129      * that the instruction was not nullified.
130      */
131     env->psw_n = 0;
132 }
133 
134 static bool hppa_cpu_has_work(CPUState *cs)
135 {
136     return cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI);
137 }
138 
139 static int hppa_cpu_mmu_index(CPUState *cs, bool ifetch)
140 {
141     CPUHPPAState *env = cpu_env(cs);
142 
143     if (env->psw & (ifetch ? PSW_C : PSW_D)) {
144         return PRIV_P_TO_MMU_IDX(env->iaoq_f & 3, env->psw & PSW_P);
145     }
146     /* mmu disabled */
147     return env->psw & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX;
148 }
149 
150 static void hppa_cpu_disas_set_info(CPUState *cs, disassemble_info *info)
151 {
152     info->mach = bfd_mach_hppa20;
153     info->print_insn = print_insn_hppa;
154 }
155 
156 #ifndef CONFIG_USER_ONLY
157 static G_NORETURN
158 void hppa_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
159                                   MMUAccessType access_type, int mmu_idx,
160                                   uintptr_t retaddr)
161 {
162     HPPACPU *cpu = HPPA_CPU(cs);
163     CPUHPPAState *env = &cpu->env;
164 
165     cs->exception_index = EXCP_UNALIGN;
166     cpu_restore_state(cs, retaddr);
167     hppa_set_ior_and_isr(env, addr, MMU_IDX_MMU_DISABLED(mmu_idx));
168 
169     cpu_loop_exit(cs);
170 }
171 #endif /* CONFIG_USER_ONLY */
172 
173 static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
174 {
175     CPUState *cs = CPU(dev);
176     HPPACPUClass *acc = HPPA_CPU_GET_CLASS(dev);
177     Error *local_err = NULL;
178 
179     cpu_exec_realizefn(cs, &local_err);
180     if (local_err != NULL) {
181         error_propagate(errp, local_err);
182         return;
183     }
184 
185     qemu_init_vcpu(cs);
186     acc->parent_realize(dev, errp);
187 
188 #ifndef CONFIG_USER_ONLY
189     {
190         HPPACPU *cpu = HPPA_CPU(cs);
191 
192         cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
193                                         hppa_cpu_alarm_timer, cpu);
194         hppa_ptlbe(&cpu->env);
195     }
196 #endif
197 
198     /* Use pc-relative instructions always to simplify the translator. */
199     tcg_cflags_set(cs, CF_PCREL);
200 }
201 
202 static void hppa_cpu_initfn(Object *obj)
203 {
204     CPUHPPAState *env = cpu_env(CPU(obj));
205 
206     env->is_pa20 = !!object_dynamic_cast(obj, TYPE_HPPA64_CPU);
207 }
208 
209 static void hppa_cpu_reset_hold(Object *obj, ResetType type)
210 {
211     HPPACPUClass *scc = HPPA_CPU_GET_CLASS(obj);
212     CPUState *cs = CPU(obj);
213     HPPACPU *cpu = HPPA_CPU(obj);
214     CPUHPPAState *env = &cpu->env;
215 
216     if (scc->parent_phases.hold) {
217         scc->parent_phases.hold(obj, type);
218     }
219     cs->exception_index = -1;
220     cs->halted = 0;
221     cpu_set_pc(cs, 0xf0000004);
222 
223     memset(env, 0, offsetof(CPUHPPAState, end_reset_fields));
224 
225     cpu_hppa_loaded_fr0(env);
226 
227     /* 64-bit machines start with space-register hashing enabled in %dr2 */
228     env->dr[2] = hppa_is_pa20(env) ? HPPA64_DIAG_SPHASH_ENABLE : 0;
229 
230     cpu_hppa_put_psw(env, PSW_M);
231 }
232 
233 static ObjectClass *hppa_cpu_class_by_name(const char *cpu_model)
234 {
235     g_autofree char *typename = g_strconcat(cpu_model, "-cpu", NULL);
236 
237     return object_class_by_name(typename);
238 }
239 
240 #ifndef CONFIG_USER_ONLY
241 #include "hw/core/sysemu-cpu-ops.h"
242 
243 static const struct SysemuCPUOps hppa_sysemu_ops = {
244     .get_phys_page_debug = hppa_cpu_get_phys_page_debug,
245 };
246 #endif
247 
248 #include "hw/core/tcg-cpu-ops.h"
249 
250 static const TCGCPUOps hppa_tcg_ops = {
251     .initialize = hppa_translate_init,
252     .translate_code = hppa_translate_code,
253     .synchronize_from_tb = hppa_cpu_synchronize_from_tb,
254     .restore_state_to_opc = hppa_restore_state_to_opc,
255 
256 #ifndef CONFIG_USER_ONLY
257     .tlb_fill_align = hppa_cpu_tlb_fill_align,
258     .cpu_exec_interrupt = hppa_cpu_exec_interrupt,
259     .cpu_exec_halt = hppa_cpu_has_work,
260     .do_interrupt = hppa_cpu_do_interrupt,
261     .do_unaligned_access = hppa_cpu_do_unaligned_access,
262     .do_transaction_failed = hppa_cpu_do_transaction_failed,
263 #endif /* !CONFIG_USER_ONLY */
264 };
265 
266 static void hppa_cpu_class_init(ObjectClass *oc, void *data)
267 {
268     DeviceClass *dc = DEVICE_CLASS(oc);
269     CPUClass *cc = CPU_CLASS(oc);
270     HPPACPUClass *acc = HPPA_CPU_CLASS(oc);
271     ResettableClass *rc = RESETTABLE_CLASS(oc);
272 
273     device_class_set_parent_realize(dc, hppa_cpu_realizefn,
274                                     &acc->parent_realize);
275 
276     resettable_class_set_parent_phases(rc, NULL, hppa_cpu_reset_hold, NULL,
277                                        &acc->parent_phases);
278 
279     cc->class_by_name = hppa_cpu_class_by_name;
280     cc->has_work = hppa_cpu_has_work;
281     cc->mmu_index = hppa_cpu_mmu_index;
282     cc->dump_state = hppa_cpu_dump_state;
283     cc->set_pc = hppa_cpu_set_pc;
284     cc->get_pc = hppa_cpu_get_pc;
285     cc->gdb_read_register = hppa_cpu_gdb_read_register;
286     cc->gdb_write_register = hppa_cpu_gdb_write_register;
287 #ifndef CONFIG_USER_ONLY
288     dc->vmsd = &vmstate_hppa_cpu;
289     cc->sysemu_ops = &hppa_sysemu_ops;
290 #endif
291     cc->disas_set_info = hppa_cpu_disas_set_info;
292     cc->gdb_num_core_regs = 128;
293     cc->tcg_ops = &hppa_tcg_ops;
294 }
295 
296 static const TypeInfo hppa_cpu_type_infos[] = {
297     {
298         .name = TYPE_HPPA_CPU,
299         .parent = TYPE_CPU,
300         .instance_size = sizeof(HPPACPU),
301         .instance_align = __alignof(HPPACPU),
302         .instance_init = hppa_cpu_initfn,
303         .abstract = false,
304         .class_size = sizeof(HPPACPUClass),
305         .class_init = hppa_cpu_class_init,
306     },
307     {
308         .name = TYPE_HPPA64_CPU,
309         .parent = TYPE_HPPA_CPU,
310     },
311 };
312 
313 DEFINE_TYPES(hppa_cpu_type_infos)
314