]> git.proxmox.com Git - qemu.git/blame - target-s390x/misc_helper.c
target-s390: Convert STIDP
[qemu.git] / target-s390x / misc_helper.c
CommitLineData
10ec5117 1/*
aea1e885 2 * S/390 misc helper routines
10ec5117 3 *
defb0e31 4 * Copyright (c) 2009 Ulrich Hecht
10ec5117
AG
5 * Copyright (c) 2009 Alexander Graf
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 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
70539e18 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
10ec5117
AG
19 */
20
3e457172 21#include "cpu.h"
022c62cb 22#include "exec/memory.h"
1de7afc9 23#include "qemu/host-utils.h"
3208afbe 24#include "helper.h"
defb0e31 25#include <string.h>
9c17d615 26#include "sysemu/kvm.h"
1de7afc9 27#include "qemu/timer.h"
af2be207
JK
28#ifdef CONFIG_KVM
29#include <linux/kvm.h>
30#endif
10ec5117 31
71e47088 32#if !defined(CONFIG_USER_ONLY)
022c62cb 33#include "exec/softmmu_exec.h"
9c17d615 34#include "sysemu/sysemu.h"
10ec5117 35#endif
d5a43964 36
defb0e31
AG
37/* #define DEBUG_HELPER */
38#ifdef DEBUG_HELPER
39#define HELPER_LOG(x...) qemu_log(x)
40#else
41#define HELPER_LOG(x...)
42#endif
43
b4e2bd35
RH
44/* Raise an exception dynamically from a helper function. */
45void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
46 uintptr_t retaddr)
47{
48 int t;
49
50 env->exception_index = EXCP_PGM;
51 env->int_pgm_code = excp;
52
53 /* Use the (ultimate) callers address to find the insn that trapped. */
54 cpu_restore_state(env, retaddr);
55
56 /* Advance past the insn. */
57 t = cpu_ldub_code(env, env->psw.addr);
58 env->int_pgm_ilen = t = get_ilen(t);
59 env->psw.addr += 2 * t;
60
61 cpu_loop_exit(env);
62}
63
d5a103cd 64/* Raise an exception statically from a TB. */
089f5c06 65void HELPER(exception)(CPUS390XState *env, uint32_t excp)
defb0e31 66{
71e47088 67 HELPER_LOG("%s: exception %d\n", __func__, excp);
defb0e31 68 env->exception_index = excp;
1162c041 69 cpu_loop_exit(env);
defb0e31
AG
70}
71
defb0e31 72#ifndef CONFIG_USER_ONLY
d5a103cd 73void program_interrupt(CPUS390XState *env, uint32_t code, int ilen)
defb0e31 74{
0d404541
RH
75 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
76 env->psw.addr);
defb0e31
AG
77
78 if (kvm_enabled()) {
af2be207 79#ifdef CONFIG_KVM
1bc22652 80 kvm_s390_interrupt(s390_env_get_cpu(env), KVM_S390_PROGRAM_INT, code);
af2be207 81#endif
defb0e31
AG
82 } else {
83 env->int_pgm_code = code;
d5a103cd 84 env->int_pgm_ilen = ilen;
defb0e31 85 env->exception_index = EXCP_PGM;
1162c041 86 cpu_loop_exit(env);
defb0e31
AG
87 }
88}
89
defb0e31 90/* SCLP service call */
089f5c06 91uint32_t HELPER(servc)(CPUS390XState *env, uint32_t r1, uint64_t r2)
defb0e31 92{
9abf567d 93 int r;
defb0e31 94
f6c98f92 95 r = sclp_service_call(r1, r2);
9abf567d
CB
96 if (r < 0) {
97 program_interrupt(env, -r, 4);
98 return 0;
99 }
100 return r;
defb0e31
AG
101}
102
103/* DIAG */
089f5c06
BS
104uint64_t HELPER(diag)(CPUS390XState *env, uint32_t num, uint64_t mem,
105 uint64_t code)
defb0e31
AG
106{
107 uint64_t r;
108
109 switch (num) {
110 case 0x500:
111 /* KVM hypercall */
112 r = s390_virtio_hypercall(env, mem, code);
113 break;
114 case 0x44:
115 /* yield */
116 r = 0;
117 break;
118 case 0x308:
119 /* ipl */
120 r = 0;
121 break;
122 default:
123 r = -1;
124 break;
125 }
126
127 if (r) {
d5a103cd 128 program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC);
defb0e31
AG
129 }
130
131 return r;
132}
133
defb0e31 134/* Set Prefix */
089f5c06 135void HELPER(spx)(CPUS390XState *env, uint64_t a1)
defb0e31
AG
136{
137 uint32_t prefix;
138
089f5c06 139 prefix = cpu_ldl_data(env, a1);
defb0e31
AG
140 env->psa = prefix & 0xfffff000;
141 qemu_log("prefix: %#x\n", prefix);
142 tlb_flush_page(env, 0);
143 tlb_flush_page(env, TARGET_PAGE_SIZE);
144}
145
146/* Set Clock */
147uint32_t HELPER(sck)(uint64_t a1)
148{
149 /* XXX not implemented - is it necessary? */
150
151 return 0;
152}
153
a4e3ad19 154static inline uint64_t clock_value(CPUS390XState *env)
defb0e31
AG
155{
156 uint64_t time;
157
158 time = env->tod_offset +
71e47088 159 time2tod(qemu_get_clock_ns(vm_clock) - env->tod_basetime);
defb0e31
AG
160
161 return time;
162}
163
164/* Store Clock */
089f5c06 165uint32_t HELPER(stck)(CPUS390XState *env, uint64_t a1)
defb0e31 166{
089f5c06 167 cpu_stq_data(env, a1, clock_value(env));
defb0e31
AG
168
169 return 0;
170}
171
172/* Store Clock Extended */
089f5c06 173uint32_t HELPER(stcke)(CPUS390XState *env, uint64_t a1)
defb0e31 174{
089f5c06 175 cpu_stb_data(env, a1, 0);
defb0e31 176 /* basically the same value as stck */
089f5c06 177 cpu_stq_data(env, a1 + 1, clock_value(env) | env->cpu_num);
defb0e31 178 /* more fine grained than stck */
089f5c06 179 cpu_stq_data(env, a1 + 9, 0);
defb0e31 180 /* XXX programmable fields */
089f5c06 181 cpu_stw_data(env, a1 + 17, 0);
defb0e31 182
defb0e31
AG
183 return 0;
184}
185
186/* Set Clock Comparator */
089f5c06 187void HELPER(sckc)(CPUS390XState *env, uint64_t a1)
defb0e31 188{
089f5c06 189 uint64_t time = cpu_ldq_data(env, a1);
defb0e31
AG
190
191 if (time == -1ULL) {
192 return;
193 }
194
195 /* difference between now and then */
196 time -= clock_value(env);
197 /* nanoseconds */
198 time = (time * 125) >> 9;
199
200 qemu_mod_timer(env->tod_timer, qemu_get_clock_ns(vm_clock) + time);
201}
202
203/* Store Clock Comparator */
089f5c06 204void HELPER(stckc)(CPUS390XState *env, uint64_t a1)
defb0e31
AG
205{
206 /* XXX implement */
089f5c06 207 cpu_stq_data(env, a1, 0);
defb0e31
AG
208}
209
210/* Set CPU Timer */
089f5c06 211void HELPER(spt)(CPUS390XState *env, uint64_t a1)
defb0e31 212{
089f5c06 213 uint64_t time = cpu_ldq_data(env, a1);
defb0e31
AG
214
215 if (time == -1ULL) {
216 return;
217 }
218
219 /* nanoseconds */
220 time = (time * 125) >> 9;
221
222 qemu_mod_timer(env->cpu_timer, qemu_get_clock_ns(vm_clock) + time);
223}
224
225/* Store CPU Timer */
089f5c06 226void HELPER(stpt)(CPUS390XState *env, uint64_t a1)
defb0e31
AG
227{
228 /* XXX implement */
089f5c06 229 cpu_stq_data(env, a1, 0);
defb0e31
AG
230}
231
232/* Store System Information */
089f5c06
BS
233uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, uint32_t r0,
234 uint32_t r1)
defb0e31
AG
235{
236 int cc = 0;
237 int sel1, sel2;
238
239 if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
240 ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
241 /* valid function code, invalid reserved bits */
242 program_interrupt(env, PGM_SPECIFICATION, 2);
243 }
244
245 sel1 = r0 & STSI_R0_SEL1_MASK;
246 sel2 = r1 & STSI_R1_SEL2_MASK;
247
248 /* XXX: spec exception if sysib is not 4k-aligned */
249
250 switch (r0 & STSI_LEVEL_MASK) {
251 case STSI_LEVEL_1:
252 if ((sel1 == 1) && (sel2 == 1)) {
253 /* Basic Machine Configuration */
254 struct sysib_111 sysib;
255
256 memset(&sysib, 0, sizeof(sysib));
257 ebcdic_put(sysib.manuf, "QEMU ", 16);
258 /* same as machine type number in STORE CPU ID */
259 ebcdic_put(sysib.type, "QEMU", 4);
260 /* same as model number in STORE CPU ID */
261 ebcdic_put(sysib.model, "QEMU ", 16);
262 ebcdic_put(sysib.sequence, "QEMU ", 16);
263 ebcdic_put(sysib.plant, "QEMU", 4);
71e47088 264 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
defb0e31
AG
265 } else if ((sel1 == 2) && (sel2 == 1)) {
266 /* Basic Machine CPU */
267 struct sysib_121 sysib;
268
269 memset(&sysib, 0, sizeof(sysib));
270 /* XXX make different for different CPUs? */
271 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
272 ebcdic_put(sysib.plant, "QEMU", 4);
273 stw_p(&sysib.cpu_addr, env->cpu_num);
71e47088 274 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
defb0e31
AG
275 } else if ((sel1 == 2) && (sel2 == 2)) {
276 /* Basic Machine CPUs */
277 struct sysib_122 sysib;
278
279 memset(&sysib, 0, sizeof(sysib));
280 stl_p(&sysib.capability, 0x443afc29);
281 /* XXX change when SMP comes */
282 stw_p(&sysib.total_cpus, 1);
283 stw_p(&sysib.active_cpus, 1);
284 stw_p(&sysib.standby_cpus, 0);
285 stw_p(&sysib.reserved_cpus, 0);
71e47088 286 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
defb0e31
AG
287 } else {
288 cc = 3;
289 }
290 break;
291 case STSI_LEVEL_2:
71e47088
BS
292 {
293 if ((sel1 == 2) && (sel2 == 1)) {
294 /* LPAR CPU */
295 struct sysib_221 sysib;
296
297 memset(&sysib, 0, sizeof(sysib));
298 /* XXX make different for different CPUs? */
299 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
300 ebcdic_put(sysib.plant, "QEMU", 4);
301 stw_p(&sysib.cpu_addr, env->cpu_num);
302 stw_p(&sysib.cpu_id, 0);
303 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
304 } else if ((sel1 == 2) && (sel2 == 2)) {
305 /* LPAR CPUs */
306 struct sysib_222 sysib;
307
308 memset(&sysib, 0, sizeof(sysib));
309 stw_p(&sysib.lpar_num, 0);
310 sysib.lcpuc = 0;
311 /* XXX change when SMP comes */
312 stw_p(&sysib.total_cpus, 1);
313 stw_p(&sysib.conf_cpus, 1);
314 stw_p(&sysib.standby_cpus, 0);
315 stw_p(&sysib.reserved_cpus, 0);
316 ebcdic_put(sysib.name, "QEMU ", 8);
317 stl_p(&sysib.caf, 1000);
318 stw_p(&sysib.dedicated_cpus, 0);
319 stw_p(&sysib.shared_cpus, 0);
320 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
321 } else {
322 cc = 3;
323 }
324 break;
defb0e31 325 }
defb0e31 326 case STSI_LEVEL_3:
71e47088
BS
327 {
328 if ((sel1 == 2) && (sel2 == 2)) {
329 /* VM CPUs */
330 struct sysib_322 sysib;
331
332 memset(&sysib, 0, sizeof(sysib));
333 sysib.count = 1;
334 /* XXX change when SMP comes */
335 stw_p(&sysib.vm[0].total_cpus, 1);
336 stw_p(&sysib.vm[0].conf_cpus, 1);
337 stw_p(&sysib.vm[0].standby_cpus, 0);
338 stw_p(&sysib.vm[0].reserved_cpus, 0);
339 ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
340 stl_p(&sysib.vm[0].caf, 1000);
341 ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16);
342 cpu_physical_memory_rw(a0, (uint8_t *)&sysib, sizeof(sysib), 1);
343 } else {
344 cc = 3;
345 }
346 break;
defb0e31 347 }
defb0e31
AG
348 case STSI_LEVEL_CURRENT:
349 env->regs[0] = STSI_LEVEL_3;
350 break;
351 default:
352 cc = 3;
353 break;
354 }
355
356 return cc;
357}
358
089f5c06
BS
359uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
360 uint64_t cpu_addr)
defb0e31
AG
361{
362 int cc = 0;
363
364 HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
71e47088 365 __func__, order_code, r1, cpu_addr);
defb0e31 366
71e47088 367 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
defb0e31
AG
368 as parameter (input). Status (output) is always R1. */
369
370 switch (order_code) {
371 case SIGP_SET_ARCH:
372 /* switch arch */
373 break;
374 case SIGP_SENSE:
375 /* enumerate CPU status */
376 if (cpu_addr) {
377 /* XXX implement when SMP comes */
378 return 3;
379 }
380 env->regs[r1] &= 0xffffffff00000000ULL;
381 cc = 1;
382 break;
71e47088 383#if !defined(CONFIG_USER_ONLY)
1864b94a
AG
384 case SIGP_RESTART:
385 qemu_system_reset_request();
386 cpu_loop_exit(env);
387 break;
388 case SIGP_STOP:
389 qemu_system_shutdown_request();
390 cpu_loop_exit(env);
391 break;
392#endif
defb0e31
AG
393 default:
394 /* unknown sigp */
395 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
396 cc = 3;
397 }
398
399 return cc;
400}
defb0e31 401#endif