root / target-ppc / kvm.c @ caa5af0f
History | View | Annotate | Download (5.2 kB)
1 | d76d1650 | aurel32 | /*
|
---|---|---|---|
2 | d76d1650 | aurel32 | * PowerPC implementation of KVM hooks
|
3 | d76d1650 | aurel32 | *
|
4 | d76d1650 | aurel32 | * Copyright IBM Corp. 2007
|
5 | d76d1650 | aurel32 | *
|
6 | d76d1650 | aurel32 | * Authors:
|
7 | d76d1650 | aurel32 | * Jerone Young <jyoung5@us.ibm.com>
|
8 | d76d1650 | aurel32 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
|
9 | d76d1650 | aurel32 | * Hollis Blanchard <hollisb@us.ibm.com>
|
10 | d76d1650 | aurel32 | *
|
11 | d76d1650 | aurel32 | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
12 | d76d1650 | aurel32 | * See the COPYING file in the top-level directory.
|
13 | d76d1650 | aurel32 | *
|
14 | d76d1650 | aurel32 | */
|
15 | d76d1650 | aurel32 | |
16 | d76d1650 | aurel32 | #include <sys/types.h> |
17 | d76d1650 | aurel32 | #include <sys/ioctl.h> |
18 | d76d1650 | aurel32 | #include <sys/mman.h> |
19 | d76d1650 | aurel32 | |
20 | d76d1650 | aurel32 | #include <linux/kvm.h> |
21 | d76d1650 | aurel32 | |
22 | d76d1650 | aurel32 | #include "qemu-common.h" |
23 | d76d1650 | aurel32 | #include "qemu-timer.h" |
24 | d76d1650 | aurel32 | #include "sysemu.h" |
25 | d76d1650 | aurel32 | #include "kvm.h" |
26 | d76d1650 | aurel32 | #include "kvm_ppc.h" |
27 | d76d1650 | aurel32 | #include "cpu.h" |
28 | d76d1650 | aurel32 | #include "device_tree.h" |
29 | d76d1650 | aurel32 | |
30 | d76d1650 | aurel32 | //#define DEBUG_KVM
|
31 | d76d1650 | aurel32 | |
32 | d76d1650 | aurel32 | #ifdef DEBUG_KVM
|
33 | d76d1650 | aurel32 | #define dprintf(fmt, ...) \
|
34 | d76d1650 | aurel32 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
35 | d76d1650 | aurel32 | #else
|
36 | d76d1650 | aurel32 | #define dprintf(fmt, ...) \
|
37 | d76d1650 | aurel32 | do { } while (0) |
38 | d76d1650 | aurel32 | #endif
|
39 | d76d1650 | aurel32 | |
40 | d76d1650 | aurel32 | int kvm_arch_init(KVMState *s, int smp_cpus) |
41 | d76d1650 | aurel32 | { |
42 | d76d1650 | aurel32 | return 0; |
43 | d76d1650 | aurel32 | } |
44 | d76d1650 | aurel32 | |
45 | d76d1650 | aurel32 | int kvm_arch_init_vcpu(CPUState *cenv)
|
46 | d76d1650 | aurel32 | { |
47 | 861bbc80 | Alexander Graf | int ret = 0; |
48 | 861bbc80 | Alexander Graf | struct kvm_sregs sregs;
|
49 | 861bbc80 | Alexander Graf | |
50 | 861bbc80 | Alexander Graf | sregs.pvr = cenv->spr[SPR_PVR]; |
51 | 861bbc80 | Alexander Graf | ret = kvm_vcpu_ioctl(cenv, KVM_SET_SREGS, &sregs); |
52 | 861bbc80 | Alexander Graf | |
53 | 861bbc80 | Alexander Graf | return ret;
|
54 | d76d1650 | aurel32 | } |
55 | d76d1650 | aurel32 | |
56 | caa5af0f | Jan Kiszka | void kvm_arch_reset_vcpu(CPUState *env)
|
57 | caa5af0f | Jan Kiszka | { |
58 | caa5af0f | Jan Kiszka | } |
59 | caa5af0f | Jan Kiszka | |
60 | d76d1650 | aurel32 | int kvm_arch_put_registers(CPUState *env)
|
61 | d76d1650 | aurel32 | { |
62 | d76d1650 | aurel32 | struct kvm_regs regs;
|
63 | d76d1650 | aurel32 | int ret;
|
64 | d76d1650 | aurel32 | int i;
|
65 | d76d1650 | aurel32 | |
66 | d76d1650 | aurel32 | ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, ®s); |
67 | d76d1650 | aurel32 | if (ret < 0) |
68 | d76d1650 | aurel32 | return ret;
|
69 | d76d1650 | aurel32 | |
70 | d76d1650 | aurel32 | regs.ctr = env->ctr; |
71 | d76d1650 | aurel32 | regs.lr = env->lr; |
72 | d76d1650 | aurel32 | regs.xer = env->xer; |
73 | d76d1650 | aurel32 | regs.msr = env->msr; |
74 | d76d1650 | aurel32 | regs.pc = env->nip; |
75 | d76d1650 | aurel32 | |
76 | d76d1650 | aurel32 | regs.srr0 = env->spr[SPR_SRR0]; |
77 | d76d1650 | aurel32 | regs.srr1 = env->spr[SPR_SRR1]; |
78 | d76d1650 | aurel32 | |
79 | d76d1650 | aurel32 | regs.sprg0 = env->spr[SPR_SPRG0]; |
80 | d76d1650 | aurel32 | regs.sprg1 = env->spr[SPR_SPRG1]; |
81 | d76d1650 | aurel32 | regs.sprg2 = env->spr[SPR_SPRG2]; |
82 | d76d1650 | aurel32 | regs.sprg3 = env->spr[SPR_SPRG3]; |
83 | d76d1650 | aurel32 | regs.sprg4 = env->spr[SPR_SPRG4]; |
84 | d76d1650 | aurel32 | regs.sprg5 = env->spr[SPR_SPRG5]; |
85 | d76d1650 | aurel32 | regs.sprg6 = env->spr[SPR_SPRG6]; |
86 | d76d1650 | aurel32 | regs.sprg7 = env->spr[SPR_SPRG7]; |
87 | d76d1650 | aurel32 | |
88 | d76d1650 | aurel32 | for (i = 0;i < 32; i++) |
89 | d76d1650 | aurel32 | regs.gpr[i] = env->gpr[i]; |
90 | d76d1650 | aurel32 | |
91 | d76d1650 | aurel32 | ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, ®s); |
92 | d76d1650 | aurel32 | if (ret < 0) |
93 | d76d1650 | aurel32 | return ret;
|
94 | d76d1650 | aurel32 | |
95 | d76d1650 | aurel32 | return ret;
|
96 | d76d1650 | aurel32 | } |
97 | d76d1650 | aurel32 | |
98 | d76d1650 | aurel32 | int kvm_arch_get_registers(CPUState *env)
|
99 | d76d1650 | aurel32 | { |
100 | d76d1650 | aurel32 | struct kvm_regs regs;
|
101 | d76d1650 | aurel32 | uint32_t i, ret; |
102 | d76d1650 | aurel32 | |
103 | d76d1650 | aurel32 | ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, ®s); |
104 | d76d1650 | aurel32 | if (ret < 0) |
105 | d76d1650 | aurel32 | return ret;
|
106 | d76d1650 | aurel32 | |
107 | d76d1650 | aurel32 | env->ctr = regs.ctr; |
108 | d76d1650 | aurel32 | env->lr = regs.lr; |
109 | d76d1650 | aurel32 | env->xer = regs.xer; |
110 | d76d1650 | aurel32 | env->msr = regs.msr; |
111 | d76d1650 | aurel32 | env->nip = regs.pc; |
112 | d76d1650 | aurel32 | |
113 | d76d1650 | aurel32 | env->spr[SPR_SRR0] = regs.srr0; |
114 | d76d1650 | aurel32 | env->spr[SPR_SRR1] = regs.srr1; |
115 | d76d1650 | aurel32 | |
116 | d76d1650 | aurel32 | env->spr[SPR_SPRG0] = regs.sprg0; |
117 | d76d1650 | aurel32 | env->spr[SPR_SPRG1] = regs.sprg1; |
118 | d76d1650 | aurel32 | env->spr[SPR_SPRG2] = regs.sprg2; |
119 | d76d1650 | aurel32 | env->spr[SPR_SPRG3] = regs.sprg3; |
120 | d76d1650 | aurel32 | env->spr[SPR_SPRG4] = regs.sprg4; |
121 | d76d1650 | aurel32 | env->spr[SPR_SPRG5] = regs.sprg5; |
122 | d76d1650 | aurel32 | env->spr[SPR_SPRG6] = regs.sprg6; |
123 | d76d1650 | aurel32 | env->spr[SPR_SPRG7] = regs.sprg7; |
124 | d76d1650 | aurel32 | |
125 | d76d1650 | aurel32 | for (i = 0;i < 32; i++) |
126 | d76d1650 | aurel32 | env->gpr[i] = regs.gpr[i]; |
127 | d76d1650 | aurel32 | |
128 | d76d1650 | aurel32 | return 0; |
129 | d76d1650 | aurel32 | } |
130 | d76d1650 | aurel32 | |
131 | 16415335 | Alexander Graf | #if defined(TARGET_PPCEMB)
|
132 | 16415335 | Alexander Graf | #define PPC_INPUT_INT PPC40x_INPUT_INT
|
133 | 16415335 | Alexander Graf | #elif defined(TARGET_PPC64)
|
134 | 16415335 | Alexander Graf | #define PPC_INPUT_INT PPC970_INPUT_INT
|
135 | 16415335 | Alexander Graf | #else
|
136 | 16415335 | Alexander Graf | #define PPC_INPUT_INT PPC6xx_INPUT_INT
|
137 | 16415335 | Alexander Graf | #endif
|
138 | 16415335 | Alexander Graf | |
139 | d76d1650 | aurel32 | int kvm_arch_pre_run(CPUState *env, struct kvm_run *run) |
140 | d76d1650 | aurel32 | { |
141 | d76d1650 | aurel32 | int r;
|
142 | d76d1650 | aurel32 | unsigned irq;
|
143 | d76d1650 | aurel32 | |
144 | d76d1650 | aurel32 | /* PowerPC Qemu tracks the various core input pins (interrupt, critical
|
145 | d76d1650 | aurel32 | * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
|
146 | d76d1650 | aurel32 | if (run->ready_for_interrupt_injection &&
|
147 | d76d1650 | aurel32 | (env->interrupt_request & CPU_INTERRUPT_HARD) && |
148 | 16415335 | Alexander Graf | (env->irq_input_state & (1<<PPC_INPUT_INT)))
|
149 | d76d1650 | aurel32 | { |
150 | d76d1650 | aurel32 | /* For now KVM disregards the 'irq' argument. However, in the
|
151 | d76d1650 | aurel32 | * future KVM could cache it in-kernel to avoid a heavyweight exit
|
152 | d76d1650 | aurel32 | * when reading the UIC.
|
153 | d76d1650 | aurel32 | */
|
154 | d76d1650 | aurel32 | irq = -1U;
|
155 | d76d1650 | aurel32 | |
156 | d76d1650 | aurel32 | dprintf("injected interrupt %d\n", irq);
|
157 | d76d1650 | aurel32 | r = kvm_vcpu_ioctl(env, KVM_INTERRUPT, &irq); |
158 | d76d1650 | aurel32 | if (r < 0) |
159 | d76d1650 | aurel32 | printf("cpu %d fail inject %x\n", env->cpu_index, irq);
|
160 | d76d1650 | aurel32 | } |
161 | d76d1650 | aurel32 | |
162 | d76d1650 | aurel32 | /* We don't know if there are more interrupts pending after this. However,
|
163 | d76d1650 | aurel32 | * the guest will return to userspace in the course of handling this one
|
164 | d76d1650 | aurel32 | * anyways, so we will get a chance to deliver the rest. */
|
165 | d76d1650 | aurel32 | return 0; |
166 | d76d1650 | aurel32 | } |
167 | d76d1650 | aurel32 | |
168 | d76d1650 | aurel32 | int kvm_arch_post_run(CPUState *env, struct kvm_run *run) |
169 | d76d1650 | aurel32 | { |
170 | d76d1650 | aurel32 | return 0; |
171 | d76d1650 | aurel32 | } |
172 | d76d1650 | aurel32 | |
173 | d76d1650 | aurel32 | static int kvmppc_handle_halt(CPUState *env) |
174 | d76d1650 | aurel32 | { |
175 | d76d1650 | aurel32 | if (!(env->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
|
176 | d76d1650 | aurel32 | env->halted = 1;
|
177 | d76d1650 | aurel32 | env->exception_index = EXCP_HLT; |
178 | d76d1650 | aurel32 | } |
179 | d76d1650 | aurel32 | |
180 | d76d1650 | aurel32 | return 1; |
181 | d76d1650 | aurel32 | } |
182 | d76d1650 | aurel32 | |
183 | d76d1650 | aurel32 | /* map dcr access to existing qemu dcr emulation */
|
184 | d76d1650 | aurel32 | static int kvmppc_handle_dcr_read(CPUState *env, uint32_t dcrn, uint32_t *data) |
185 | d76d1650 | aurel32 | { |
186 | d76d1650 | aurel32 | if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) |
187 | d76d1650 | aurel32 | fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
|
188 | d76d1650 | aurel32 | |
189 | d76d1650 | aurel32 | return 1; |
190 | d76d1650 | aurel32 | } |
191 | d76d1650 | aurel32 | |
192 | d76d1650 | aurel32 | static int kvmppc_handle_dcr_write(CPUState *env, uint32_t dcrn, uint32_t data) |
193 | d76d1650 | aurel32 | { |
194 | d76d1650 | aurel32 | if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) |
195 | d76d1650 | aurel32 | fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
|
196 | d76d1650 | aurel32 | |
197 | d76d1650 | aurel32 | return 1; |
198 | d76d1650 | aurel32 | } |
199 | d76d1650 | aurel32 | |
200 | d76d1650 | aurel32 | int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run) |
201 | d76d1650 | aurel32 | { |
202 | d76d1650 | aurel32 | int ret = 0; |
203 | d76d1650 | aurel32 | |
204 | d76d1650 | aurel32 | switch (run->exit_reason) {
|
205 | d76d1650 | aurel32 | case KVM_EXIT_DCR:
|
206 | d76d1650 | aurel32 | if (run->dcr.is_write) {
|
207 | d76d1650 | aurel32 | dprintf("handle dcr write\n");
|
208 | d76d1650 | aurel32 | ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data); |
209 | d76d1650 | aurel32 | } else {
|
210 | d76d1650 | aurel32 | dprintf("handle dcr read\n");
|
211 | d76d1650 | aurel32 | ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data); |
212 | d76d1650 | aurel32 | } |
213 | d76d1650 | aurel32 | break;
|
214 | d76d1650 | aurel32 | case KVM_EXIT_HLT:
|
215 | d76d1650 | aurel32 | dprintf("handle halt\n");
|
216 | d76d1650 | aurel32 | ret = kvmppc_handle_halt(env); |
217 | d76d1650 | aurel32 | break;
|
218 | d76d1650 | aurel32 | } |
219 | d76d1650 | aurel32 | |
220 | d76d1650 | aurel32 | return ret;
|
221 | d76d1650 | aurel32 | } |