2 * PowerPC implementation of KVM hooks
4 * Copyright IBM Corp. 2007
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>
20 #include <linux/kvm.h>
22 #include "qemu-common.h"
23 #include "qemu-timer.h"
28 #include "device_tree.h"
33 #define dprintf(fmt, ...) \
34 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
36 #define dprintf(fmt, ...) \
40 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
45 int kvm_arch_init_vcpu(CPUState
*cenv
)
48 struct kvm_sregs sregs
;
50 sregs
.pvr
= cenv
->spr
[SPR_PVR
];
51 ret
= kvm_vcpu_ioctl(cenv
, KVM_SET_SREGS
, &sregs
);
56 void kvm_arch_reset_vcpu(CPUState
*env
)
60 int kvm_arch_put_registers(CPUState
*env
)
66 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
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
, ®s
);
98 int kvm_arch_get_registers(CPUState
*env
)
100 struct kvm_regs regs
;
103 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
113 env
->spr
[SPR_SRR0
] = regs
.srr0
;
114 env
->spr
[SPR_SRR1
] = regs
.srr1
;
116 env
->spr
[SPR_SPRG0
] = regs
.sprg0
;
117 env
->spr
[SPR_SPRG1
] = regs
.sprg1
;
118 env
->spr
[SPR_SPRG2
] = regs
.sprg2
;
119 env
->spr
[SPR_SPRG3
] = regs
.sprg3
;
120 env
->spr
[SPR_SPRG4
] = regs
.sprg4
;
121 env
->spr
[SPR_SPRG5
] = regs
.sprg5
;
122 env
->spr
[SPR_SPRG6
] = regs
.sprg6
;
123 env
->spr
[SPR_SPRG7
] = regs
.sprg7
;
125 for (i
= 0;i
< 32; i
++)
126 env
->gpr
[i
] = regs
.gpr
[i
];
131 #if defined(TARGET_PPCEMB)
132 #define PPC_INPUT_INT PPC40x_INPUT_INT
133 #elif defined(TARGET_PPC64)
134 #define PPC_INPUT_INT PPC970_INPUT_INT
136 #define PPC_INPUT_INT PPC6xx_INPUT_INT
139 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
144 /* PowerPC Qemu tracks the various core input pins (interrupt, critical
145 * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
146 if (run
->ready_for_interrupt_injection
&&
147 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
148 (env
->irq_input_state
& (1<<PPC_INPUT_INT
)))
150 /* For now KVM disregards the 'irq' argument. However, in the
151 * future KVM could cache it in-kernel to avoid a heavyweight exit
152 * when reading the UIC.
156 dprintf("injected interrupt %d\n", irq
);
157 r
= kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &irq
);
159 printf("cpu %d fail inject %x\n", env
->cpu_index
, irq
);
162 /* We don't know if there are more interrupts pending after this. However,
163 * the guest will return to userspace in the course of handling this one
164 * anyways, so we will get a chance to deliver the rest. */
168 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
173 static int kvmppc_handle_halt(CPUState
*env
)
175 if (!(env
->interrupt_request
& CPU_INTERRUPT_HARD
) && (msr_ee
)) {
177 env
->exception_index
= EXCP_HLT
;
183 /* map dcr access to existing qemu dcr emulation */
184 static int kvmppc_handle_dcr_read(CPUState
*env
, uint32_t dcrn
, uint32_t *data
)
186 if (ppc_dcr_read(env
->dcr_env
, dcrn
, data
) < 0)
187 fprintf(stderr
, "Read to unhandled DCR (0x%x)\n", dcrn
);
192 static int kvmppc_handle_dcr_write(CPUState
*env
, uint32_t dcrn
, uint32_t data
)
194 if (ppc_dcr_write(env
->dcr_env
, dcrn
, data
) < 0)
195 fprintf(stderr
, "Write to unhandled DCR (0x%x)\n", dcrn
);
200 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
204 switch (run
->exit_reason
) {
206 if (run
->dcr
.is_write
) {
207 dprintf("handle dcr write\n");
208 ret
= kvmppc_handle_dcr_write(env
, run
->dcr
.dcrn
, run
->dcr
.data
);
210 dprintf("handle dcr read\n");
211 ret
= kvmppc_handle_dcr_read(env
, run
->dcr
.dcrn
, &run
->dcr
.data
);
215 dprintf("handle halt\n");
216 ret
= kvmppc_handle_halt(env
);