4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2011 Alexander Graf
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 of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
24 #include "exec/gdbstub.h"
25 #include "qemu/timer.h"
26 #include "exec/exec-all.h"
27 #include "hw/s390x/ioinst.h"
28 #ifndef CONFIG_USER_ONLY
29 #include "sysemu/sysemu.h"
33 //#define DEBUG_S390_STDOUT
36 #ifdef DEBUG_S390_STDOUT
37 #define DPRINTF(fmt, ...) \
38 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
39 if (qemu_log_separate()) qemu_log(fmt, ##__VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...) \
42 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
45 #define DPRINTF(fmt, ...) \
50 #ifndef CONFIG_USER_ONLY
51 void s390x_tod_timer(void *opaque
)
53 S390CPU
*cpu
= opaque
;
54 CPUS390XState
*env
= &cpu
->env
;
56 env
->pending_int
|= INTERRUPT_TOD
;
57 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_HARD
);
60 void s390x_cpu_timer(void *opaque
)
62 S390CPU
*cpu
= opaque
;
63 CPUS390XState
*env
= &cpu
->env
;
65 env
->pending_int
|= INTERRUPT_CPUTIMER
;
66 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_HARD
);
70 S390CPU
*cpu_s390x_create(const char *cpu_model
, Error
**errp
)
72 static bool features_parsed
;
73 char *name
, *features
;
78 name
= g_strdup(cpu_model
);
79 features
= strchr(name
, ',');
85 oc
= cpu_class_by_name(TYPE_S390_CPU
, name
);
87 error_setg(errp
, "Unknown CPU definition \'%s\'", name
);
91 typename
= object_class_get_name(oc
);
93 if (!features_parsed
) {
94 features_parsed
= true;
96 cc
->parse_features(typename
, features
, errp
);
103 return S390_CPU(CPU(object_new(typename
)));
106 S390CPU
*s390x_new_cpu(const char *cpu_model
, int64_t id
, Error
**errp
)
111 cpu
= cpu_s390x_create(cpu_model
, &err
);
116 object_property_set_int(OBJECT(cpu
), id
, "id", &err
);
120 object_property_set_bool(OBJECT(cpu
), true, "realized", &err
);
124 error_propagate(errp
, err
);
125 object_unref(OBJECT(cpu
));
131 S390CPU
*cpu_s390x_init(const char *cpu_model
)
135 /* Use to track CPU ID for linux-user only */
136 static int64_t next_cpu_id
;
138 cpu
= s390x_new_cpu(cpu_model
, next_cpu_id
++, &err
);
140 error_report_err(err
);
145 #ifndef CONFIG_USER_ONLY
147 hwaddr
s390_cpu_get_phys_page_debug(CPUState
*cs
, vaddr vaddr
)
149 S390CPU
*cpu
= S390_CPU(cs
);
150 CPUS390XState
*env
= &cpu
->env
;
153 uint64_t asc
= env
->psw
.mask
& PSW_MASK_ASC
;
156 if (!(env
->psw
.mask
& PSW_MASK_64
)) {
160 if (mmu_translate(env
, vaddr
, MMU_INST_FETCH
, asc
, &raddr
, &prot
, false)) {
166 hwaddr
s390_cpu_get_phys_addr_debug(CPUState
*cs
, vaddr vaddr
)
171 page
= vaddr
& TARGET_PAGE_MASK
;
172 phys_addr
= cpu_get_phys_page_debug(cs
, page
);
173 phys_addr
+= (vaddr
& ~TARGET_PAGE_MASK
);
178 void load_psw(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
180 uint64_t old_mask
= env
->psw
.mask
;
182 env
->psw
.addr
= addr
;
183 env
->psw
.mask
= mask
;
185 env
->cc_op
= (mask
>> 44) & 3;
188 if ((old_mask
^ mask
) & PSW_MASK_PER
) {
189 s390_cpu_recompute_watchpoints(CPU(s390_env_get_cpu(env
)));
192 if (mask
& PSW_MASK_WAIT
) {
193 S390CPU
*cpu
= s390_env_get_cpu(env
);
194 if (s390_cpu_halt(cpu
) == 0) {
195 #ifndef CONFIG_USER_ONLY
196 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
202 uint64_t get_psw_mask(CPUS390XState
*env
)
204 uint64_t r
= env
->psw
.mask
;
207 env
->cc_op
= calc_cc(env
, env
->cc_op
, env
->cc_src
, env
->cc_dst
,
211 assert(!(env
->cc_op
& ~3));
212 r
|= (uint64_t)env
->cc_op
<< 44;
218 LowCore
*cpu_map_lowcore(CPUS390XState
*env
)
220 S390CPU
*cpu
= s390_env_get_cpu(env
);
222 hwaddr len
= sizeof(LowCore
);
224 lowcore
= cpu_physical_memory_map(env
->psa
, &len
, 1);
226 if (len
< sizeof(LowCore
)) {
227 cpu_abort(CPU(cpu
), "Could not map lowcore\n");
233 void cpu_unmap_lowcore(LowCore
*lowcore
)
235 cpu_physical_memory_unmap(lowcore
, sizeof(LowCore
), 1, sizeof(LowCore
));
238 void do_restart_interrupt(CPUS390XState
*env
)
243 lowcore
= cpu_map_lowcore(env
);
245 lowcore
->restart_old_psw
.mask
= cpu_to_be64(get_psw_mask(env
));
246 lowcore
->restart_old_psw
.addr
= cpu_to_be64(env
->psw
.addr
);
247 mask
= be64_to_cpu(lowcore
->restart_new_psw
.mask
);
248 addr
= be64_to_cpu(lowcore
->restart_new_psw
.addr
);
250 cpu_unmap_lowcore(lowcore
);
252 load_psw(env
, mask
, addr
);
255 void s390_cpu_recompute_watchpoints(CPUState
*cs
)
257 const int wp_flags
= BP_CPU
| BP_MEM_WRITE
| BP_STOP_BEFORE_ACCESS
;
258 S390CPU
*cpu
= S390_CPU(cs
);
259 CPUS390XState
*env
= &cpu
->env
;
261 /* We are called when the watchpoints have changed. First
263 cpu_watchpoint_remove_all(cs
, BP_CPU
);
265 /* Return if PER is not enabled */
266 if (!(env
->psw
.mask
& PSW_MASK_PER
)) {
270 /* Return if storage-alteration event is not enabled. */
271 if (!(env
->cregs
[9] & PER_CR9_EVENT_STORE
)) {
275 if (env
->cregs
[10] == 0 && env
->cregs
[11] == -1LL) {
276 /* We can't create a watchoint spanning the whole memory range, so
277 split it in two parts. */
278 cpu_watchpoint_insert(cs
, 0, 1ULL << 63, wp_flags
, NULL
);
279 cpu_watchpoint_insert(cs
, 1ULL << 63, 1ULL << 63, wp_flags
, NULL
);
280 } else if (env
->cregs
[10] > env
->cregs
[11]) {
281 /* The address range loops, create two watchpoints. */
282 cpu_watchpoint_insert(cs
, env
->cregs
[10], -env
->cregs
[10],
284 cpu_watchpoint_insert(cs
, 0, env
->cregs
[11] + 1, wp_flags
, NULL
);
287 /* Default case, create a single watchpoint. */
288 cpu_watchpoint_insert(cs
, env
->cregs
[10],
289 env
->cregs
[11] - env
->cregs
[10] + 1,
294 #endif /* CONFIG_USER_ONLY */
296 void s390_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
299 S390CPU
*cpu
= S390_CPU(cs
);
300 CPUS390XState
*env
= &cpu
->env
;
303 if (env
->cc_op
> 3) {
304 cpu_fprintf(f
, "PSW=mask %016" PRIx64
" addr %016" PRIx64
" cc %15s\n",
305 env
->psw
.mask
, env
->psw
.addr
, cc_name(env
->cc_op
));
307 cpu_fprintf(f
, "PSW=mask %016" PRIx64
" addr %016" PRIx64
" cc %02x\n",
308 env
->psw
.mask
, env
->psw
.addr
, env
->cc_op
);
311 for (i
= 0; i
< 16; i
++) {
312 cpu_fprintf(f
, "R%02d=%016" PRIx64
, i
, env
->regs
[i
]);
314 cpu_fprintf(f
, "\n");
320 for (i
= 0; i
< 16; i
++) {
321 cpu_fprintf(f
, "F%02d=%016" PRIx64
, i
, get_freg(env
, i
)->ll
);
323 cpu_fprintf(f
, "\n");
329 for (i
= 0; i
< 32; i
++) {
330 cpu_fprintf(f
, "V%02d=%016" PRIx64
"%016" PRIx64
, i
,
331 env
->vregs
[i
][0].ll
, env
->vregs
[i
][1].ll
);
332 cpu_fprintf(f
, (i
% 2) ? "\n" : " ");
335 #ifndef CONFIG_USER_ONLY
336 for (i
= 0; i
< 16; i
++) {
337 cpu_fprintf(f
, "C%02d=%016" PRIx64
, i
, env
->cregs
[i
]);
339 cpu_fprintf(f
, "\n");
346 #ifdef DEBUG_INLINE_BRANCHES
347 for (i
= 0; i
< CC_OP_MAX
; i
++) {
348 cpu_fprintf(f
, " %15s = %10ld\t%10ld\n", cc_name(i
),
349 inline_branch_miss
[i
], inline_branch_hit
[i
]);
353 cpu_fprintf(f
, "\n");