xref: /qemu/target/alpha/cpu.c (revision 641f1c53862aec64810c0b93b5b1de49d55fda92)
1 /*
2  * QEMU Alpha CPU
3  *
4  * Copyright (c) 2007 Jocelyn Mayer
5  * Copyright (c) 2012 SUSE LINUX Products GmbH
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see
19  * <http://www.gnu.org/licenses/lgpl-2.1.html>
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "qemu/qemu-print.h"
25 #include "cpu.h"
26 #include "exec/exec-all.h"
27 #include "exec/translation-block.h"
28 #include "exec/target_page.h"
29 #include "fpu/softfloat.h"
30 
31 
32 static void alpha_cpu_set_pc(CPUState *cs, vaddr value)
33 {
34     CPUAlphaState *env = cpu_env(cs);
35     env->pc = value;
36 }
37 
38 static vaddr alpha_cpu_get_pc(CPUState *cs)
39 {
40     CPUAlphaState *env = cpu_env(cs);
41     return env->pc;
42 }
43 
44 static void alpha_cpu_synchronize_from_tb(CPUState *cs,
45                                           const TranslationBlock *tb)
46 {
47     /* The program counter is always up to date with CF_PCREL. */
48     if (!(tb_cflags(tb) & CF_PCREL)) {
49         CPUAlphaState *env = cpu_env(cs);
50         env->pc = tb->pc;
51     }
52 }
53 
54 static void alpha_restore_state_to_opc(CPUState *cs,
55                                        const TranslationBlock *tb,
56                                        const uint64_t *data)
57 {
58     CPUAlphaState *env = cpu_env(cs);
59 
60     if (tb_cflags(tb) & CF_PCREL) {
61         env->pc = (env->pc & TARGET_PAGE_MASK) | data[0];
62     } else {
63         env->pc = data[0];
64     }
65 }
66 
67 #ifndef CONFIG_USER_ONLY
68 static bool alpha_cpu_has_work(CPUState *cs)
69 {
70     /* Here we are checking to see if the CPU should wake up from HALT.
71        We will have gotten into this state only for WTINT from PALmode.  */
72     /* ??? I'm not sure how the IPL state works with WTINT to keep a CPU
73        asleep even if (some) interrupts have been asserted.  For now,
74        assume that if a CPU really wants to stay asleep, it will mask
75        interrupts at the chipset level, which will prevent these bits
76        from being set in the first place.  */
77     return cs->interrupt_request & (CPU_INTERRUPT_HARD
78                                     | CPU_INTERRUPT_TIMER
79                                     | CPU_INTERRUPT_SMP
80                                     | CPU_INTERRUPT_MCHK);
81 }
82 #endif /* !CONFIG_USER_ONLY */
83 
84 static int alpha_cpu_mmu_index(CPUState *cs, bool ifetch)
85 {
86     return alpha_env_mmu_index(cpu_env(cs));
87 }
88 
89 static void alpha_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
90 {
91     info->endian = BFD_ENDIAN_LITTLE;
92     info->mach = bfd_mach_alpha_ev6;
93     info->print_insn = print_insn_alpha;
94 }
95 
96 static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
97 {
98     CPUState *cs = CPU(dev);
99     AlphaCPUClass *acc = ALPHA_CPU_GET_CLASS(dev);
100     Error *local_err = NULL;
101 
102 #ifndef CONFIG_USER_ONLY
103     /* Use pc-relative instructions in system-mode */
104     cs->tcg_cflags |= CF_PCREL;
105 #endif
106 
107     cpu_exec_realizefn(cs, &local_err);
108     if (local_err != NULL) {
109         error_propagate(errp, local_err);
110         return;
111     }
112 
113     qemu_init_vcpu(cs);
114 
115     acc->parent_realize(dev, errp);
116 }
117 
118 /* Models */
119 typedef struct AlphaCPUAlias {
120     const char *alias;
121     const char *typename;
122 } AlphaCPUAlias;
123 
124 static const AlphaCPUAlias alpha_cpu_aliases[] = {
125     { "21064",   ALPHA_CPU_TYPE_NAME("ev4") },
126     { "21164",   ALPHA_CPU_TYPE_NAME("ev5") },
127     { "21164a",  ALPHA_CPU_TYPE_NAME("ev56") },
128     { "21164pc", ALPHA_CPU_TYPE_NAME("pca56") },
129     { "21264",   ALPHA_CPU_TYPE_NAME("ev6") },
130     { "21264a",  ALPHA_CPU_TYPE_NAME("ev67") },
131 };
132 
133 static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
134 {
135     ObjectClass *oc;
136     char *typename;
137     int i;
138 
139     oc = object_class_by_name(cpu_model);
140     if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL) {
141         return oc;
142     }
143 
144     for (i = 0; i < ARRAY_SIZE(alpha_cpu_aliases); i++) {
145         if (strcmp(cpu_model, alpha_cpu_aliases[i].alias) == 0) {
146             oc = object_class_by_name(alpha_cpu_aliases[i].typename);
147             assert(oc != NULL && !object_class_is_abstract(oc));
148             return oc;
149         }
150     }
151 
152     typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
153     oc = object_class_by_name(typename);
154     g_free(typename);
155 
156     return oc;
157 }
158 
159 static void ev4_cpu_initfn(Object *obj)
160 {
161     cpu_env(CPU(obj))->implver = IMPLVER_2106x;
162 }
163 
164 static void ev5_cpu_initfn(Object *obj)
165 {
166     cpu_env(CPU(obj))->implver = IMPLVER_21164;
167 }
168 
169 static void ev56_cpu_initfn(Object *obj)
170 {
171     cpu_env(CPU(obj))->amask |= AMASK_BWX;
172 }
173 
174 static void pca56_cpu_initfn(Object *obj)
175 {
176     cpu_env(CPU(obj))->amask |= AMASK_MVI;
177 }
178 
179 static void ev6_cpu_initfn(Object *obj)
180 {
181     CPUAlphaState *env = cpu_env(CPU(obj));
182 
183     env->implver = IMPLVER_21264;
184     env->amask = AMASK_BWX | AMASK_FIX | AMASK_MVI | AMASK_TRAP;
185 }
186 
187 static void ev67_cpu_initfn(Object *obj)
188 {
189     cpu_env(CPU(obj))->amask |= AMASK_CIX | AMASK_PREFETCH;
190 }
191 
192 static void alpha_cpu_initfn(Object *obj)
193 {
194     CPUAlphaState *env = cpu_env(CPU(obj));
195 
196     /* TODO all this should be done in reset, not init */
197 
198     env->lock_addr = -1;
199 
200     /*
201      * TODO: this is incorrect. The Alpha Architecture Handbook version 4
202      * describes NaN propagation in section 4.7.10.4. We should prefer
203      * the operand in Fb (whether it is a QNaN or an SNaN), then the
204      * operand in Fa. That is float_2nan_prop_ba.
205      */
206     set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status);
207     /* Default NaN: sign bit clear, msb frac bit set */
208     set_float_default_nan_pattern(0b01000000, &env->fp_status);
209     /*
210      * TODO: this is incorrect. The Alpha Architecture Handbook version 4
211      * section 4.7.7.11 says that we flush to zero for underflow cases, so
212      * this should be float_ftz_after_rounding to match the
213      * tininess_after_rounding (which is specified in section 4.7.5).
214      */
215     set_float_ftz_detection(float_ftz_before_rounding, &env->fp_status);
216 #if defined(CONFIG_USER_ONLY)
217     env->flags = ENV_FLAG_PS_USER | ENV_FLAG_FEN;
218     cpu_alpha_store_fpcr(env, (uint64_t)(FPCR_INVD | FPCR_DZED | FPCR_OVFD
219                                          | FPCR_UNFD | FPCR_INED | FPCR_DNOD
220                                          | FPCR_DYN_NORMAL) << 32);
221 #else
222     env->flags = ENV_FLAG_PAL_MODE | ENV_FLAG_FEN;
223 #endif
224 }
225 
226 #ifndef CONFIG_USER_ONLY
227 #include "hw/core/sysemu-cpu-ops.h"
228 
229 static const struct SysemuCPUOps alpha_sysemu_ops = {
230     .has_work = alpha_cpu_has_work,
231     .get_phys_page_debug = alpha_cpu_get_phys_page_debug,
232 };
233 #endif
234 
235 #include "accel/tcg/cpu-ops.h"
236 
237 static const TCGCPUOps alpha_tcg_ops = {
238     /* Alpha processors have a weak memory model */
239     .guest_default_memory_order = 0,
240     .mttcg_supported = true,
241 
242     .initialize = alpha_translate_init,
243     .translate_code = alpha_translate_code,
244     .synchronize_from_tb = alpha_cpu_synchronize_from_tb,
245     .restore_state_to_opc = alpha_restore_state_to_opc,
246     .mmu_index = alpha_cpu_mmu_index,
247 
248 #ifdef CONFIG_USER_ONLY
249     .record_sigsegv = alpha_cpu_record_sigsegv,
250     .record_sigbus = alpha_cpu_record_sigbus,
251 #else
252     .tlb_fill = alpha_cpu_tlb_fill,
253     .cpu_exec_interrupt = alpha_cpu_exec_interrupt,
254     .cpu_exec_halt = alpha_cpu_has_work,
255     .do_interrupt = alpha_cpu_do_interrupt,
256     .do_transaction_failed = alpha_cpu_do_transaction_failed,
257     .do_unaligned_access = alpha_cpu_do_unaligned_access,
258 #endif /* !CONFIG_USER_ONLY */
259 };
260 
261 static void alpha_cpu_class_init(ObjectClass *oc, const void *data)
262 {
263     DeviceClass *dc = DEVICE_CLASS(oc);
264     CPUClass *cc = CPU_CLASS(oc);
265     AlphaCPUClass *acc = ALPHA_CPU_CLASS(oc);
266 
267     device_class_set_parent_realize(dc, alpha_cpu_realizefn,
268                                     &acc->parent_realize);
269 
270     cc->class_by_name = alpha_cpu_class_by_name;
271     cc->dump_state = alpha_cpu_dump_state;
272     cc->set_pc = alpha_cpu_set_pc;
273     cc->get_pc = alpha_cpu_get_pc;
274     cc->gdb_read_register = alpha_cpu_gdb_read_register;
275     cc->gdb_write_register = alpha_cpu_gdb_write_register;
276 #ifndef CONFIG_USER_ONLY
277     dc->vmsd = &vmstate_alpha_cpu;
278     cc->sysemu_ops = &alpha_sysemu_ops;
279 #endif
280     cc->disas_set_info = alpha_cpu_disas_set_info;
281 
282     cc->tcg_ops = &alpha_tcg_ops;
283     cc->gdb_num_core_regs = 67;
284 }
285 
286 #define DEFINE_ALPHA_CPU_TYPE(base_type, cpu_model, initfn) \
287      {                                                      \
288          .parent = base_type,                               \
289          .instance_init = initfn,                           \
290          .name = ALPHA_CPU_TYPE_NAME(cpu_model),            \
291      }
292 
293 static const TypeInfo alpha_cpu_type_infos[] = {
294     {
295         .name = TYPE_ALPHA_CPU,
296         .parent = TYPE_CPU,
297         .instance_size = sizeof(AlphaCPU),
298         .instance_align = __alignof(AlphaCPU),
299         .instance_init = alpha_cpu_initfn,
300         .abstract = true,
301         .class_size = sizeof(AlphaCPUClass),
302         .class_init = alpha_cpu_class_init,
303     },
304     DEFINE_ALPHA_CPU_TYPE(TYPE_ALPHA_CPU, "ev4", ev4_cpu_initfn),
305     DEFINE_ALPHA_CPU_TYPE(TYPE_ALPHA_CPU, "ev5", ev5_cpu_initfn),
306     DEFINE_ALPHA_CPU_TYPE(ALPHA_CPU_TYPE_NAME("ev5"), "ev56", ev56_cpu_initfn),
307     DEFINE_ALPHA_CPU_TYPE(ALPHA_CPU_TYPE_NAME("ev56"), "pca56",
308                           pca56_cpu_initfn),
309     DEFINE_ALPHA_CPU_TYPE(TYPE_ALPHA_CPU, "ev6", ev6_cpu_initfn),
310     DEFINE_ALPHA_CPU_TYPE(ALPHA_CPU_TYPE_NAME("ev6"), "ev67", ev67_cpu_initfn),
311     DEFINE_ALPHA_CPU_TYPE(ALPHA_CPU_TYPE_NAME("ev67"), "ev68", NULL),
312 };
313 
314 DEFINE_TYPES(alpha_cpu_type_infos)
315