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