monitor: Accept input only byte-wise
[qemu/aliguori-queue.git] / target-ppc / kvm.c
blob2b5abf430e4069a61efa7f7f1805cb1947171cb0
1 /*
2 * PowerPC implementation of KVM hooks
4 * Copyright IBM Corp. 2007
6 * Authors:
7 * Jerone Young <jyoung5@us.ibm.com>
8 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9 * Hollis Blanchard <hollisb@us.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
20 #include <linux/kvm.h>
22 #include "qemu-common.h"
23 #include "qemu-timer.h"
24 #include "sysemu.h"
25 #include "kvm.h"
26 #include "kvm_ppc.h"
27 #include "cpu.h"
28 #include "device_tree.h"
30 //#define DEBUG_KVM
32 #ifdef DEBUG_KVM
33 #define dprintf(fmt, ...) \
34 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
35 #else
36 #define dprintf(fmt, ...) \
37 do { } while (0)
38 #endif
40 int kvm_arch_init(KVMState *s, int smp_cpus)
42 return 0;
45 int kvm_arch_init_vcpu(CPUState *cenv)
47 int ret = 0;
48 struct kvm_sregs sregs;
50 sregs.pvr = cenv->spr[SPR_PVR];
51 ret = kvm_vcpu_ioctl(cenv, KVM_SET_SREGS, &sregs);
53 return ret;
56 void kvm_arch_reset_vcpu(CPUState *env)
60 int kvm_arch_put_registers(CPUState *env)
62 struct kvm_regs regs;
63 int ret;
64 int i;
66 ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
67 if (ret < 0)
68 return ret;
70 regs.ctr = env->ctr;
71 regs.lr = env->lr;
72 regs.xer = env->xer;
73 regs.msr = env->msr;
74 regs.pc = env->nip;
76 regs.srr0 = env->spr[SPR_SRR0];
77 regs.srr1 = env->spr[SPR_SRR1];
79 regs.sprg0 = env->spr[SPR_SPRG0];
80 regs.sprg1 = env->spr[SPR_SPRG1];
81 regs.sprg2 = env->spr[SPR_SPRG2];
82 regs.sprg3 = env->spr[SPR_SPRG3];
83 regs.sprg4 = env->spr[SPR_SPRG4];
84 regs.sprg5 = env->spr[SPR_SPRG5];
85 regs.sprg6 = env->spr[SPR_SPRG6];
86 regs.sprg7 = env->spr[SPR_SPRG7];
88 for (i = 0;i < 32; i++)
89 regs.gpr[i] = env->gpr[i];
91 ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, &regs);
92 if (ret < 0)
93 return ret;
95 return ret;
98 int kvm_arch_get_registers(CPUState *env)
100 struct kvm_regs regs;
101 struct kvm_sregs sregs;
102 uint32_t i, ret;
104 ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
105 if (ret < 0)
106 return ret;
108 ret = kvm_vcpu_ioctl(env, KVM_GET_SREGS, &sregs);
109 if (ret < 0)
110 return ret;
112 env->ctr = regs.ctr;
113 env->lr = regs.lr;
114 env->xer = regs.xer;
115 env->msr = regs.msr;
116 env->nip = regs.pc;
118 env->spr[SPR_SRR0] = regs.srr0;
119 env->spr[SPR_SRR1] = regs.srr1;
121 env->spr[SPR_SPRG0] = regs.sprg0;
122 env->spr[SPR_SPRG1] = regs.sprg1;
123 env->spr[SPR_SPRG2] = regs.sprg2;
124 env->spr[SPR_SPRG3] = regs.sprg3;
125 env->spr[SPR_SPRG4] = regs.sprg4;
126 env->spr[SPR_SPRG5] = regs.sprg5;
127 env->spr[SPR_SPRG6] = regs.sprg6;
128 env->spr[SPR_SPRG7] = regs.sprg7;
130 for (i = 0;i < 32; i++)
131 env->gpr[i] = regs.gpr[i];
133 #ifdef KVM_CAP_PPC_SEGSTATE
134 if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_SEGSTATE)) {
135 env->sdr1 = sregs.u.s.sdr1;
137 /* Sync SLB */
138 for (i = 0; i < 64; i++) {
139 ppc_store_slb(env, sregs.u.s.ppc64.slb[i].slbe,
140 sregs.u.s.ppc64.slb[i].slbv);
143 /* Sync SRs */
144 for (i = 0; i < 16; i++) {
145 env->sr[i] = sregs.u.s.ppc32.sr[i];
148 /* Sync BATs */
149 for (i = 0; i < 8; i++) {
150 env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
151 env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
152 env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
153 env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
156 #endif
158 return 0;
161 #if defined(TARGET_PPCEMB)
162 #define PPC_INPUT_INT PPC40x_INPUT_INT
163 #elif defined(TARGET_PPC64)
164 #define PPC_INPUT_INT PPC970_INPUT_INT
165 #else
166 #define PPC_INPUT_INT PPC6xx_INPUT_INT
167 #endif
169 int kvm_arch_pre_run(CPUState *env, struct kvm_run *run)
171 int r;
172 unsigned irq;
174 /* PowerPC Qemu tracks the various core input pins (interrupt, critical
175 * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
176 if (run->ready_for_interrupt_injection &&
177 (env->interrupt_request & CPU_INTERRUPT_HARD) &&
178 (env->irq_input_state & (1<<PPC_INPUT_INT)))
180 /* For now KVM disregards the 'irq' argument. However, in the
181 * future KVM could cache it in-kernel to avoid a heavyweight exit
182 * when reading the UIC.
184 irq = -1U;
186 dprintf("injected interrupt %d\n", irq);
187 r = kvm_vcpu_ioctl(env, KVM_INTERRUPT, &irq);
188 if (r < 0)
189 printf("cpu %d fail inject %x\n", env->cpu_index, irq);
192 /* We don't know if there are more interrupts pending after this. However,
193 * the guest will return to userspace in the course of handling this one
194 * anyways, so we will get a chance to deliver the rest. */
195 return 0;
198 int kvm_arch_post_run(CPUState *env, struct kvm_run *run)
200 return 0;
203 static int kvmppc_handle_halt(CPUState *env)
205 if (!(env->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
206 env->halted = 1;
207 env->exception_index = EXCP_HLT;
210 return 1;
213 /* map dcr access to existing qemu dcr emulation */
214 static int kvmppc_handle_dcr_read(CPUState *env, uint32_t dcrn, uint32_t *data)
216 if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
217 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
219 return 1;
222 static int kvmppc_handle_dcr_write(CPUState *env, uint32_t dcrn, uint32_t data)
224 if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
225 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
227 return 1;
230 int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
232 int ret = 0;
234 switch (run->exit_reason) {
235 case KVM_EXIT_DCR:
236 if (run->dcr.is_write) {
237 dprintf("handle dcr write\n");
238 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
239 } else {
240 dprintf("handle dcr read\n");
241 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
243 break;
244 case KVM_EXIT_HLT:
245 dprintf("handle halt\n");
246 ret = kvmppc_handle_halt(env);
247 break;
250 return ret;