xref: /kvmtool/powerpc/kvm-cpu.c (revision 63e158a0a0a05551e8e983826bb26a2c0a78ef4b)
1 /*
2  * PPC64 processor support
3  *
4  * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation.
9  */
10 
11 #include "kvm/kvm-cpu.h"
12 
13 #include "kvm/symbol.h"
14 #include "kvm/util.h"
15 #include "kvm/kvm.h"
16 
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <signal.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdio.h>
24 
25 static int debug_fd;
26 
27 void kvm_cpu__set_debug_fd(int fd)
28 {
29 	debug_fd = fd;
30 }
31 
32 int kvm_cpu__get_debug_fd(void)
33 {
34 	return debug_fd;
35 }
36 
37 static struct kvm_cpu *kvm_cpu__new(struct kvm *kvm)
38 {
39 	struct kvm_cpu *vcpu;
40 
41 	vcpu		= calloc(1, sizeof *vcpu);
42 	if (!vcpu)
43 		return NULL;
44 
45 	vcpu->kvm	= kvm;
46 
47 	return vcpu;
48 }
49 
50 void kvm_cpu__delete(struct kvm_cpu *vcpu)
51 {
52 	free(vcpu);
53 }
54 
55 struct kvm_cpu *kvm_cpu__init(struct kvm *kvm, unsigned long cpu_id)
56 {
57 	struct kvm_cpu *vcpu;
58 	int mmap_size;
59 	struct kvm_enable_cap papr_cap = { .cap = KVM_CAP_PPC_PAPR };
60 
61 	vcpu		= kvm_cpu__new(kvm);
62 	if (!vcpu)
63 		return NULL;
64 
65 	vcpu->cpu_id	= cpu_id;
66 
67 	vcpu->vcpu_fd = ioctl(vcpu->kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
68 	if (vcpu->vcpu_fd < 0)
69 		die_perror("KVM_CREATE_VCPU ioctl");
70 
71 	mmap_size = ioctl(vcpu->kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
72 	if (mmap_size < 0)
73 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
74 
75 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, vcpu->vcpu_fd, 0);
76 	if (vcpu->kvm_run == MAP_FAILED)
77 		die("unable to mmap vcpu fd");
78 
79 	ioctl(vcpu->vcpu_fd, KVM_ENABLE_CAP, &papr_cap);
80 
81 	/*
82 	 * We start all CPUs, directing non-primary threads into the kernel's
83 	 * secondary start point.  When we come to support SLOF, we will start
84 	 * only one and SLOF will RTAS call us to ask for others to be
85 	 * started.  (FIXME: make more generic & interface with whichever
86 	 * firmware a platform may be using.)
87 	 */
88 	vcpu->is_running = true;
89 
90 	return vcpu;
91 }
92 
93 static void kvm_cpu__setup_fpu(struct kvm_cpu *vcpu)
94 {
95 	/* Don't have to do anything, there's no expected FPU state. */
96 }
97 
98 static void kvm_cpu__setup_regs(struct kvm_cpu *vcpu)
99 {
100 	/*
101 	 * FIXME: This assumes PPC64 and Linux guest.  It doesn't use the
102 	 * OpenFirmware entry method, but instead the "embedded" entry which
103 	 * passes the FDT address directly.
104 	 */
105 	struct kvm_regs *r = &vcpu->regs;
106 
107 	if (vcpu->cpu_id == 0) {
108 		r->pc = KERNEL_START_ADDR;
109 		r->gpr[3] = vcpu->kvm->fdt_gra;
110 		r->gpr[5] = 0;
111 	} else {
112 		r->pc = KERNEL_SECONDARY_START_ADDR;
113 		r->gpr[3] = vcpu->cpu_id;
114 	}
115 	r->msr = 0x8000000000001000UL; /* 64bit, non-HV, ME */
116 
117 	if (ioctl(vcpu->vcpu_fd, KVM_SET_REGS, &vcpu->regs) < 0)
118 		die_perror("KVM_SET_REGS failed");
119 }
120 
121 static void kvm_cpu__setup_sregs(struct kvm_cpu *vcpu)
122 {
123 	/*
124 	 * No sregs setup is required on PPC64/SPAPR (but there may be setup
125 	 * required for non-paravirtualised platforms, e.g. TLB/SLB setup).
126 	 */
127 }
128 
129 /**
130  * kvm_cpu__reset_vcpu - reset virtual CPU to a known state
131  */
132 void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
133 {
134 	kvm_cpu__setup_regs(vcpu);
135 	kvm_cpu__setup_sregs(vcpu);
136 	kvm_cpu__setup_fpu(vcpu);
137 }
138 
139 /* kvm_cpu__irq - set KVM's IRQ flag on this vcpu */
140 void kvm_cpu__irq(struct kvm_cpu *vcpu, int pin, int level)
141 {
142 }
143 
144 void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
145 {
146 }
147 
148 bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
149 {
150 	bool ret = true;
151 	struct kvm_run *run = vcpu->kvm_run;
152 	switch(run->exit_reason) {
153 	default:
154 		ret = false;
155 	}
156 	return ret;
157 }
158 
159 #define CONDSTR_BIT(m, b) (((m) & MSR_##b) ? #b" " : "")
160 
161 void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
162 {
163 	struct kvm_regs regs;
164 	struct kvm_sregs sregs;
165 	int r;
166 
167 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &regs) < 0)
168 		die("KVM_GET_REGS failed");
169         if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &sregs) < 0)
170 		die("KVM_GET_SREGS failed");
171 
172 	dprintf(debug_fd, "\n Registers:\n");
173 	dprintf(debug_fd, " NIP:   %016llx  MSR:   %016llx "
174 		"( %s%s%s%s%s%s%s%s%s%s%s%s)\n",
175 		regs.pc, regs.msr,
176 		CONDSTR_BIT(regs.msr, SF),
177 		CONDSTR_BIT(regs.msr, HV), /* ! */
178 		CONDSTR_BIT(regs.msr, VEC),
179 		CONDSTR_BIT(regs.msr, VSX),
180 		CONDSTR_BIT(regs.msr, EE),
181 		CONDSTR_BIT(regs.msr, PR),
182 		CONDSTR_BIT(regs.msr, FP),
183 		CONDSTR_BIT(regs.msr, ME),
184 		CONDSTR_BIT(regs.msr, IR),
185 		CONDSTR_BIT(regs.msr, DR),
186 		CONDSTR_BIT(regs.msr, RI),
187 		CONDSTR_BIT(regs.msr, LE));
188 	dprintf(debug_fd, " CTR:   %016llx  LR:    %016llx  CR:   %08llx\n",
189 		regs.ctr, regs.lr, regs.cr);
190 	dprintf(debug_fd, " SRR0:  %016llx  SRR1:  %016llx  XER:  %016llx\n",
191 		regs.srr0, regs.srr1, regs.xer);
192 	dprintf(debug_fd, " SPRG0: %016llx  SPRG1: %016llx\n",
193 		regs.sprg0, regs.sprg1);
194 	dprintf(debug_fd, " SPRG2: %016llx  SPRG3: %016llx\n",
195 		regs.sprg2, regs.sprg3);
196 	dprintf(debug_fd, " SPRG4: %016llx  SPRG5: %016llx\n",
197 		regs.sprg4, regs.sprg5);
198 	dprintf(debug_fd, " SPRG6: %016llx  SPRG7: %016llx\n",
199 		regs.sprg6, regs.sprg7);
200 	dprintf(debug_fd, " GPRs:\n ");
201 	for (r = 0; r < 32; r++) {
202 		dprintf(debug_fd, "%016llx  ", regs.gpr[r]);
203 		if ((r & 3) == 3)
204 			dprintf(debug_fd, "\n ");
205 	}
206 	dprintf(debug_fd, "\n");
207 
208 	/* FIXME: Assumes SLB-based (book3s) guest */
209 	for (r = 0; r < 32; r++) {
210 		dprintf(debug_fd, " SLB%02d  %016llx %016llx\n", r,
211 			sregs.u.s.ppc64.slb[r].slbe,
212 			sregs.u.s.ppc64.slb[r].slbv);
213 	}
214 	dprintf(debug_fd, "----------\n");
215 }
216 
217 void kvm_cpu__show_code(struct kvm_cpu *vcpu)
218 {
219 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &vcpu->regs) < 0)
220 		die("KVM_GET_REGS failed");
221 
222 	/* FIXME: Dump/disassemble some code...! */
223 
224 	dprintf(debug_fd, "\n Stack:\n");
225 	dprintf(debug_fd,   " ------\n");
226 	/* Only works in real mode: */
227 	kvm__dump_mem(vcpu->kvm, vcpu->regs.gpr[1], 32);
228 }
229 
230 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
231 {
232 	/* Does nothing yet */
233 }
234