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