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