Merge branch 'develop' into tizen_qemu_1.5.1
[sdk/emulator/qemu.git] / target-s390x / kvm.c
1 /*
2  * QEMU S390x KVM implementation
3  *
4  * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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 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 <http://www.gnu.org/licenses/>.
18  */
19
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/mman.h>
23
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
26
27 #include "qemu-common.h"
28 #include "qemu-timer.h"
29 #include "sysemu.h"
30 #include "kvm.h"
31 #include "cpu.h"
32 #include "device_tree.h"
33
34 /* #define DEBUG_KVM */
35
36 #ifdef DEBUG_KVM
37 #define dprintf(fmt, ...) \
38     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
39 #else
40 #define dprintf(fmt, ...) \
41     do { } while (0)
42 #endif
43
44 #define IPA0_DIAG                       0x8300
45 #define IPA0_SIGP                       0xae00
46 #define IPA0_PRIV                       0xb200
47
48 #define PRIV_SCLP_CALL                  0x20
49 #define DIAG_KVM_HYPERCALL              0x500
50 #define DIAG_KVM_BREAKPOINT             0x501
51
52 #define ICPT_INSTRUCTION                0x04
53 #define ICPT_WAITPSW                    0x1c
54 #define ICPT_SOFT_INTERCEPT             0x24
55 #define ICPT_CPU_STOP                   0x28
56 #define ICPT_IO                         0x40
57
58 #define SIGP_RESTART                    0x06
59 #define SIGP_INITIAL_CPU_RESET          0x0b
60 #define SIGP_STORE_STATUS_ADDR          0x0e
61 #define SIGP_SET_ARCH                   0x12
62
63 #define SCLP_CMDW_READ_SCP_INFO         0x00020001
64 #define SCLP_CMDW_READ_SCP_INFO_FORCED  0x00120001
65
66 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
67     KVM_CAP_LAST_INFO
68 };
69
70 int kvm_arch_init(KVMState *s)
71 {
72     return 0;
73 }
74
75 int kvm_arch_init_vcpu(CPUS390XState *env)
76 {
77     int ret = 0;
78
79     if (kvm_vcpu_ioctl(env, KVM_S390_INITIAL_RESET, NULL) < 0) {
80         perror("cannot init reset vcpu");
81     }
82
83     return ret;
84 }
85
86 void kvm_arch_reset_vcpu(CPUS390XState *env)
87 {
88     /* FIXME: add code to reset vcpu. */
89 }
90
91 int kvm_arch_put_registers(CPUS390XState *env, int level)
92 {
93     struct kvm_regs regs;
94     int ret;
95     int i;
96
97     ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
98     if (ret < 0) {
99         return ret;
100     }
101
102     for (i = 0; i < 16; i++) {
103         regs.gprs[i] = env->regs[i];
104     }
105
106     ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, &regs);
107     if (ret < 0) {
108         return ret;
109     }
110
111     env->kvm_run->psw_addr = env->psw.addr;
112     env->kvm_run->psw_mask = env->psw.mask;
113
114     return ret;
115 }
116
117 int kvm_arch_get_registers(CPUS390XState *env)
118 {
119     int ret;
120     struct kvm_regs regs;
121     int i;
122
123     ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
124     if (ret < 0) {
125         return ret;
126     }
127
128     for (i = 0; i < 16; i++) {
129         env->regs[i] = regs.gprs[i];
130     }
131
132     env->psw.addr = env->kvm_run->psw_addr;
133     env->psw.mask = env->kvm_run->psw_mask;
134
135     return 0;
136 }
137
138 /*
139  * Legacy layout for s390:
140  * Older S390 KVM requires the topmost vma of the RAM to be
141  * smaller than an system defined value, which is at least 256GB.
142  * Larger systems have larger values. We put the guest between
143  * the end of data segment (system break) and this value. We
144  * use 32GB as a base to have enough room for the system break
145  * to grow. We also have to use MAP parameters that avoid
146  * read-only mapping of guest pages.
147  */
148 static void *legacy_s390_alloc(ram_addr_t size)
149 {
150     void *mem;
151
152     mem = mmap((void *) 0x800000000ULL, size,
153                PROT_EXEC|PROT_READ|PROT_WRITE,
154                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
155     if (mem == MAP_FAILED) {
156         fprintf(stderr, "Allocating RAM failed\n");
157         abort();
158     }
159     return mem;
160 }
161
162 void *kvm_arch_vmalloc(ram_addr_t size)
163 {
164     /* Can we use the standard allocation ? */
165     if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) &&
166         kvm_check_extension(kvm_state, KVM_CAP_S390_COW)) {
167         return NULL;
168     } else {
169         return legacy_s390_alloc(size);
170     }
171 }
172
173 int kvm_arch_insert_sw_breakpoint(CPUS390XState *env, struct kvm_sw_breakpoint *bp)
174 {
175     static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
176
177     if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
178         cpu_memory_rw_debug(env, bp->pc, (uint8_t *)diag_501, 4, 1)) {
179         return -EINVAL;
180     }
181     return 0;
182 }
183
184 int kvm_arch_remove_sw_breakpoint(CPUS390XState *env, struct kvm_sw_breakpoint *bp)
185 {
186     uint8_t t[4];
187     static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
188
189     if (cpu_memory_rw_debug(env, bp->pc, t, 4, 0)) {
190         return -EINVAL;
191     } else if (memcmp(t, diag_501, 4)) {
192         return -EINVAL;
193     } else if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
194         return -EINVAL;
195     }
196
197     return 0;
198 }
199
200 void kvm_arch_pre_run(CPUS390XState *env, struct kvm_run *run)
201 {
202 }
203
204 void kvm_arch_post_run(CPUS390XState *env, struct kvm_run *run)
205 {
206 }
207
208 int kvm_arch_process_async_events(CPUS390XState *env)
209 {
210     return env->halted;
211 }
212
213 void kvm_s390_interrupt_internal(CPUS390XState *env, int type, uint32_t parm,
214                                  uint64_t parm64, int vm)
215 {
216     struct kvm_s390_interrupt kvmint;
217     int r;
218
219     if (!env->kvm_state) {
220         return;
221     }
222
223     kvmint.type = type;
224     kvmint.parm = parm;
225     kvmint.parm64 = parm64;
226
227     if (vm) {
228         r = kvm_vm_ioctl(env->kvm_state, KVM_S390_INTERRUPT, &kvmint);
229     } else {
230         r = kvm_vcpu_ioctl(env, KVM_S390_INTERRUPT, &kvmint);
231     }
232
233     if (r < 0) {
234         fprintf(stderr, "KVM failed to inject interrupt\n");
235         exit(1);
236     }
237 }
238
239 void kvm_s390_virtio_irq(CPUS390XState *env, int config_change, uint64_t token)
240 {
241     kvm_s390_interrupt_internal(env, KVM_S390_INT_VIRTIO, config_change,
242                                 token, 1);
243 }
244
245 void kvm_s390_interrupt(CPUS390XState *env, int type, uint32_t code)
246 {
247     kvm_s390_interrupt_internal(env, type, code, 0, 0);
248 }
249
250 static void enter_pgmcheck(CPUS390XState *env, uint16_t code)
251 {
252     kvm_s390_interrupt(env, KVM_S390_PROGRAM_INT, code);
253 }
254
255 static inline void setcc(CPUS390XState *env, uint64_t cc)
256 {
257     env->kvm_run->psw_mask &= ~(3ull << 44);
258     env->kvm_run->psw_mask |= (cc & 3) << 44;
259
260     env->psw.mask &= ~(3ul << 44);
261     env->psw.mask |= (cc & 3) << 44;
262 }
263
264 static int kvm_sclp_service_call(CPUS390XState *env, struct kvm_run *run,
265                                  uint16_t ipbh0)
266 {
267     uint32_t sccb;
268     uint64_t code;
269     int r = 0;
270
271     cpu_synchronize_state(env);
272     sccb = env->regs[ipbh0 & 0xf];
273     code = env->regs[(ipbh0 & 0xf0) >> 4];
274
275     r = sclp_service_call(env, sccb, code);
276     if (r < 0) {
277         enter_pgmcheck(env, -r);
278     }
279     setcc(env, r);
280
281     return 0;
282 }
283
284 static int handle_priv(CPUS390XState *env, struct kvm_run *run, uint8_t ipa1)
285 {
286     int r = 0;
287     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
288
289     dprintf("KVM: PRIV: %d\n", ipa1);
290     switch (ipa1) {
291         case PRIV_SCLP_CALL:
292             r = kvm_sclp_service_call(env, run, ipbh0);
293             break;
294         default:
295             dprintf("KVM: unknown PRIV: 0x%x\n", ipa1);
296             r = -1;
297             break;
298     }
299
300     return r;
301 }
302
303 static int handle_hypercall(CPUS390XState *env, struct kvm_run *run)
304 {
305     cpu_synchronize_state(env);
306     env->regs[2] = s390_virtio_hypercall(env, env->regs[2], env->regs[1]);
307
308     return 0;
309 }
310
311 static int handle_diag(CPUS390XState *env, struct kvm_run *run, int ipb_code)
312 {
313     int r = 0;
314
315     switch (ipb_code) {
316         case DIAG_KVM_HYPERCALL:
317             r = handle_hypercall(env, run);
318             break;
319         case DIAG_KVM_BREAKPOINT:
320             sleep(10);
321             break;
322         default:
323             dprintf("KVM: unknown DIAG: 0x%x\n", ipb_code);
324             r = -1;
325             break;
326     }
327
328     return r;
329 }
330
331 static int s390_cpu_restart(S390CPU *cpu)
332 {
333     CPUS390XState *env = &cpu->env;
334
335     kvm_s390_interrupt(env, KVM_S390_RESTART, 0);
336     s390_add_running_cpu(env);
337     qemu_cpu_kick(env);
338     dprintf("DONE: SIGP cpu restart: %p\n", env);
339     return 0;
340 }
341
342 static int s390_store_status(CPUS390XState *env, uint32_t parameter)
343 {
344     /* XXX */
345     fprintf(stderr, "XXX SIGP store status\n");
346     return -1;
347 }
348
349 static int s390_cpu_initial_reset(CPUS390XState *env)
350 {
351     int i;
352
353     s390_del_running_cpu(env);
354     if (kvm_vcpu_ioctl(env, KVM_S390_INITIAL_RESET, NULL) < 0) {
355         perror("cannot init reset vcpu");
356     }
357
358     /* Manually zero out all registers */
359     cpu_synchronize_state(env);
360     for (i = 0; i < 16; i++) {
361         env->regs[i] = 0;
362     }
363
364     dprintf("DONE: SIGP initial reset: %p\n", env);
365     return 0;
366 }
367
368 static int handle_sigp(CPUS390XState *env, struct kvm_run *run, uint8_t ipa1)
369 {
370     uint8_t order_code;
371     uint32_t parameter;
372     uint16_t cpu_addr;
373     uint8_t t;
374     int r = -1;
375     S390CPU *target_cpu;
376     CPUS390XState *target_env;
377
378     cpu_synchronize_state(env);
379
380     /* get order code */
381     order_code = run->s390_sieic.ipb >> 28;
382     if (order_code > 0) {
383         order_code = env->regs[order_code];
384     }
385     order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
386
387     /* get parameters */
388     t = (ipa1 & 0xf0) >> 4;
389     if (!(t % 2)) {
390         t++;
391     }
392
393     parameter = env->regs[t] & 0x7ffffe00;
394     cpu_addr = env->regs[ipa1 & 0x0f];
395
396     target_cpu = s390_cpu_addr2state(cpu_addr);
397     if (target_cpu == NULL) {
398         goto out;
399     }
400     target_env = &target_cpu->env;
401
402     switch (order_code) {
403         case SIGP_RESTART:
404             r = s390_cpu_restart(target_cpu);
405             break;
406         case SIGP_STORE_STATUS_ADDR:
407             r = s390_store_status(target_env, parameter);
408             break;
409         case SIGP_SET_ARCH:
410             /* make the caller panic */
411             return -1;
412         case SIGP_INITIAL_CPU_RESET:
413             r = s390_cpu_initial_reset(target_env);
414             break;
415         default:
416             fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
417             break;
418     }
419
420 out:
421     setcc(env, r ? 3 : 0);
422     return 0;
423 }
424
425 static int handle_instruction(CPUS390XState *env, struct kvm_run *run)
426 {
427     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
428     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
429     int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
430     int r = -1;
431
432     dprintf("handle_instruction 0x%x 0x%x\n", run->s390_sieic.ipa, run->s390_sieic.ipb);
433     switch (ipa0) {
434         case IPA0_PRIV:
435             r = handle_priv(env, run, ipa1);
436             break;
437         case IPA0_DIAG:
438             r = handle_diag(env, run, ipb_code);
439             break;
440         case IPA0_SIGP:
441             r = handle_sigp(env, run, ipa1);
442             break;
443     }
444
445     if (r < 0) {
446         enter_pgmcheck(env, 0x0001);
447     }
448     return 0;
449 }
450
451 static bool is_special_wait_psw(CPUS390XState *env)
452 {
453     /* signal quiesce */
454     return env->kvm_run->psw_addr == 0xfffUL;
455 }
456
457 static int handle_intercept(CPUS390XState *env)
458 {
459     struct kvm_run *run = env->kvm_run;
460     int icpt_code = run->s390_sieic.icptcode;
461     int r = 0;
462
463     dprintf("intercept: 0x%x (at 0x%lx)\n", icpt_code,
464             (long)env->kvm_run->psw_addr);
465     switch (icpt_code) {
466         case ICPT_INSTRUCTION:
467             r = handle_instruction(env, run);
468             break;
469         case ICPT_WAITPSW:
470             if (s390_del_running_cpu(env) == 0 &&
471                 is_special_wait_psw(env)) {
472                 qemu_system_shutdown_request();
473             }
474             r = EXCP_HALTED;
475             break;
476         case ICPT_CPU_STOP:
477             if (s390_del_running_cpu(env) == 0) {
478                 qemu_system_shutdown_request();
479             }
480             r = EXCP_HALTED;
481             break;
482         case ICPT_SOFT_INTERCEPT:
483             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
484             exit(1);
485             break;
486         case ICPT_IO:
487             fprintf(stderr, "KVM unimplemented icpt IO\n");
488             exit(1);
489             break;
490         default:
491             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
492             exit(1);
493             break;
494     }
495
496     return r;
497 }
498
499 int kvm_arch_handle_exit(CPUS390XState *env, struct kvm_run *run)
500 {
501     int ret = 0;
502
503     switch (run->exit_reason) {
504         case KVM_EXIT_S390_SIEIC:
505             ret = handle_intercept(env);
506             break;
507         case KVM_EXIT_S390_RESET:
508             qemu_system_reset_request();
509             break;
510         default:
511             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
512             break;
513     }
514
515     if (ret == 0) {
516         ret = EXCP_INTERRUPT;
517     }
518     return ret;
519 }
520
521 bool kvm_arch_stop_on_emulation_error(CPUS390XState *env)
522 {
523     return true;
524 }
525
526 int kvm_arch_on_sigbus_vcpu(CPUS390XState *env, int code, void *addr)
527 {
528     return 1;
529 }
530
531 int kvm_arch_on_sigbus(int code, void *addr)
532 {
533     return 1;
534 }