blob: e93681753deb81e1a20b76a061e557a969ecb5bb [file] [log] [blame]
Alexander Graf2a342ed2010-07-29 14:47:48 +02001/*
2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2, as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/init.h>
23#include <linux/kvm_para.h>
24#include <linux/slab.h>
25#include <linux/of.h>
26
27#include <asm/reg.h>
28#include <asm/kvm_ppc.h>
29#include <asm/sections.h>
30#include <asm/cacheflush.h>
31#include <asm/disassemble.h>
32
Alexander Grafd17051c2010-07-29 14:47:57 +020033#define KVM_MAGIC_PAGE (-4096L)
34#define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
35
Alexander Grafd1293c92010-07-29 14:47:59 +020036#define KVM_INST_LWZ 0x80000000
37#define KVM_INST_STW 0x90000000
38#define KVM_INST_LD 0xe8000000
39#define KVM_INST_STD 0xf8000000
40#define KVM_INST_NOP 0x60000000
41#define KVM_INST_B 0x48000000
42#define KVM_INST_B_MASK 0x03ffffff
43#define KVM_INST_B_MAX 0x01ffffff
44
Alexander Graf73a18102010-07-29 14:47:58 +020045#define KVM_MASK_RT 0x03e00000
Alexander Grafd1293c92010-07-29 14:47:59 +020046#define KVM_INST_MFMSR 0x7c0000a6
47#define KVM_INST_MFSPR_SPRG0 0x7c1042a6
48#define KVM_INST_MFSPR_SPRG1 0x7c1142a6
49#define KVM_INST_MFSPR_SPRG2 0x7c1242a6
50#define KVM_INST_MFSPR_SPRG3 0x7c1342a6
51#define KVM_INST_MFSPR_SRR0 0x7c1a02a6
52#define KVM_INST_MFSPR_SRR1 0x7c1b02a6
53#define KVM_INST_MFSPR_DAR 0x7c1302a6
54#define KVM_INST_MFSPR_DSISR 0x7c1202a6
55
56#define KVM_INST_MTSPR_SPRG0 0x7c1043a6
57#define KVM_INST_MTSPR_SPRG1 0x7c1143a6
58#define KVM_INST_MTSPR_SPRG2 0x7c1243a6
59#define KVM_INST_MTSPR_SPRG3 0x7c1343a6
60#define KVM_INST_MTSPR_SRR0 0x7c1a03a6
61#define KVM_INST_MTSPR_SRR1 0x7c1b03a6
62#define KVM_INST_MTSPR_DAR 0x7c1303a6
63#define KVM_INST_MTSPR_DSISR 0x7c1203a6
Alexander Graf73a18102010-07-29 14:47:58 +020064
Alexander Grafd1290b152010-07-29 14:48:00 +020065#define KVM_INST_TLBSYNC 0x7c00046c
Alexander Graf78109272010-07-29 14:48:05 +020066#define KVM_INST_MTMSRD_L0 0x7c000164
Alexander Graf819a63d2010-07-29 14:48:04 +020067#define KVM_INST_MTMSRD_L1 0x7c010164
Alexander Graf78109272010-07-29 14:48:05 +020068#define KVM_INST_MTMSR 0x7c000124
Alexander Grafd1290b152010-07-29 14:48:00 +020069
Alexander Graf644bfa02010-07-29 14:48:06 +020070#define KVM_INST_WRTEEI_0 0x7c000146
71#define KVM_INST_WRTEEI_1 0x7c008146
72
Alexander Graf73a18102010-07-29 14:47:58 +020073static bool kvm_patching_worked = true;
Alexander Graf2d4f5672010-07-29 14:48:01 +020074static char kvm_tmp[1024 * 1024];
75static int kvm_tmp_index;
Alexander Graf73a18102010-07-29 14:47:58 +020076
77static inline void kvm_patch_ins(u32 *inst, u32 new_inst)
78{
79 *inst = new_inst;
80 flush_icache_range((ulong)inst, (ulong)inst + 4);
81}
82
Alexander Grafd1293c92010-07-29 14:47:59 +020083static void kvm_patch_ins_ld(u32 *inst, long addr, u32 rt)
84{
85#ifdef CONFIG_64BIT
86 kvm_patch_ins(inst, KVM_INST_LD | rt | (addr & 0x0000fffc));
87#else
88 kvm_patch_ins(inst, KVM_INST_LWZ | rt | ((addr + 4) & 0x0000fffc));
89#endif
90}
91
92static void kvm_patch_ins_lwz(u32 *inst, long addr, u32 rt)
93{
94 kvm_patch_ins(inst, KVM_INST_LWZ | rt | (addr & 0x0000ffff));
95}
96
97static void kvm_patch_ins_std(u32 *inst, long addr, u32 rt)
98{
99#ifdef CONFIG_64BIT
100 kvm_patch_ins(inst, KVM_INST_STD | rt | (addr & 0x0000fffc));
101#else
102 kvm_patch_ins(inst, KVM_INST_STW | rt | ((addr + 4) & 0x0000fffc));
103#endif
104}
105
106static void kvm_patch_ins_stw(u32 *inst, long addr, u32 rt)
107{
108 kvm_patch_ins(inst, KVM_INST_STW | rt | (addr & 0x0000fffc));
109}
110
Alexander Grafd1290b152010-07-29 14:48:00 +0200111static void kvm_patch_ins_nop(u32 *inst)
112{
113 kvm_patch_ins(inst, KVM_INST_NOP);
114}
115
Alexander Graf71ee8e32010-07-29 14:48:02 +0200116static void kvm_patch_ins_b(u32 *inst, int addr)
117{
118#ifdef CONFIG_RELOCATABLE
119 /* On relocatable kernels interrupts handlers and our code
120 can be in different regions, so we don't patch them */
121
122 extern u32 __end_interrupts;
123 if ((ulong)inst < (ulong)&__end_interrupts)
124 return;
125#endif
126
127 kvm_patch_ins(inst, KVM_INST_B | (addr & KVM_INST_B_MASK));
128}
129
Alexander Graf2d4f5672010-07-29 14:48:01 +0200130static u32 *kvm_alloc(int len)
131{
132 u32 *p;
133
134 if ((kvm_tmp_index + len) > ARRAY_SIZE(kvm_tmp)) {
135 printk(KERN_ERR "KVM: No more space (%d + %d)\n",
136 kvm_tmp_index, len);
137 kvm_patching_worked = false;
138 return NULL;
139 }
140
141 p = (void*)&kvm_tmp[kvm_tmp_index];
142 kvm_tmp_index += len;
143
144 return p;
145}
146
Alexander Graf819a63d2010-07-29 14:48:04 +0200147extern u32 kvm_emulate_mtmsrd_branch_offs;
148extern u32 kvm_emulate_mtmsrd_reg_offs;
149extern u32 kvm_emulate_mtmsrd_len;
150extern u32 kvm_emulate_mtmsrd[];
151
152static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)
153{
154 u32 *p;
155 int distance_start;
156 int distance_end;
157 ulong next_inst;
158
159 p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
160 if (!p)
161 return;
162
163 /* Find out where we are and put everything there */
164 distance_start = (ulong)p - (ulong)inst;
165 next_inst = ((ulong)inst + 4);
166 distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs];
167
168 /* Make sure we only write valid b instructions */
169 if (distance_start > KVM_INST_B_MAX) {
170 kvm_patching_worked = false;
171 return;
172 }
173
174 /* Modify the chunk to fit the invocation */
175 memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
176 p[kvm_emulate_mtmsrd_branch_offs] |= distance_end & KVM_INST_B_MASK;
177 p[kvm_emulate_mtmsrd_reg_offs] |= rt;
178 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsrd_len * 4);
179
180 /* Patch the invocation */
181 kvm_patch_ins_b(inst, distance_start);
182}
183
Alexander Graf78109272010-07-29 14:48:05 +0200184extern u32 kvm_emulate_mtmsr_branch_offs;
185extern u32 kvm_emulate_mtmsr_reg1_offs;
186extern u32 kvm_emulate_mtmsr_reg2_offs;
187extern u32 kvm_emulate_mtmsr_reg3_offs;
188extern u32 kvm_emulate_mtmsr_orig_ins_offs;
189extern u32 kvm_emulate_mtmsr_len;
190extern u32 kvm_emulate_mtmsr[];
191
192static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)
193{
194 u32 *p;
195 int distance_start;
196 int distance_end;
197 ulong next_inst;
198
199 p = kvm_alloc(kvm_emulate_mtmsr_len * 4);
200 if (!p)
201 return;
202
203 /* Find out where we are and put everything there */
204 distance_start = (ulong)p - (ulong)inst;
205 next_inst = ((ulong)inst + 4);
206 distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsr_branch_offs];
207
208 /* Make sure we only write valid b instructions */
209 if (distance_start > KVM_INST_B_MAX) {
210 kvm_patching_worked = false;
211 return;
212 }
213
214 /* Modify the chunk to fit the invocation */
215 memcpy(p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len * 4);
216 p[kvm_emulate_mtmsr_branch_offs] |= distance_end & KVM_INST_B_MASK;
217 p[kvm_emulate_mtmsr_reg1_offs] |= rt;
218 p[kvm_emulate_mtmsr_reg2_offs] |= rt;
219 p[kvm_emulate_mtmsr_reg3_offs] |= rt;
220 p[kvm_emulate_mtmsr_orig_ins_offs] = *inst;
221 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsr_len * 4);
222
223 /* Patch the invocation */
224 kvm_patch_ins_b(inst, distance_start);
225}
226
Alexander Graf644bfa02010-07-29 14:48:06 +0200227#ifdef CONFIG_BOOKE
228
229extern u32 kvm_emulate_wrteei_branch_offs;
230extern u32 kvm_emulate_wrteei_ee_offs;
231extern u32 kvm_emulate_wrteei_len;
232extern u32 kvm_emulate_wrteei[];
233
234static void kvm_patch_ins_wrteei(u32 *inst)
235{
236 u32 *p;
237 int distance_start;
238 int distance_end;
239 ulong next_inst;
240
241 p = kvm_alloc(kvm_emulate_wrteei_len * 4);
242 if (!p)
243 return;
244
245 /* Find out where we are and put everything there */
246 distance_start = (ulong)p - (ulong)inst;
247 next_inst = ((ulong)inst + 4);
248 distance_end = next_inst - (ulong)&p[kvm_emulate_wrteei_branch_offs];
249
250 /* Make sure we only write valid b instructions */
251 if (distance_start > KVM_INST_B_MAX) {
252 kvm_patching_worked = false;
253 return;
254 }
255
256 /* Modify the chunk to fit the invocation */
257 memcpy(p, kvm_emulate_wrteei, kvm_emulate_wrteei_len * 4);
258 p[kvm_emulate_wrteei_branch_offs] |= distance_end & KVM_INST_B_MASK;
259 p[kvm_emulate_wrteei_ee_offs] |= (*inst & MSR_EE);
260 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrteei_len * 4);
261
262 /* Patch the invocation */
263 kvm_patch_ins_b(inst, distance_start);
264}
265
266#endif
267
Alexander Graf73a18102010-07-29 14:47:58 +0200268static void kvm_map_magic_page(void *data)
269{
270 kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
271 KVM_MAGIC_PAGE, /* Physical Address */
272 KVM_MAGIC_PAGE); /* Effective Address */
273}
274
275static void kvm_check_ins(u32 *inst)
276{
277 u32 _inst = *inst;
278 u32 inst_no_rt = _inst & ~KVM_MASK_RT;
279 u32 inst_rt = _inst & KVM_MASK_RT;
280
281 switch (inst_no_rt) {
Alexander Grafd1293c92010-07-29 14:47:59 +0200282 /* Loads */
283 case KVM_INST_MFMSR:
284 kvm_patch_ins_ld(inst, magic_var(msr), inst_rt);
285 break;
286 case KVM_INST_MFSPR_SPRG0:
287 kvm_patch_ins_ld(inst, magic_var(sprg0), inst_rt);
288 break;
289 case KVM_INST_MFSPR_SPRG1:
290 kvm_patch_ins_ld(inst, magic_var(sprg1), inst_rt);
291 break;
292 case KVM_INST_MFSPR_SPRG2:
293 kvm_patch_ins_ld(inst, magic_var(sprg2), inst_rt);
294 break;
295 case KVM_INST_MFSPR_SPRG3:
296 kvm_patch_ins_ld(inst, magic_var(sprg3), inst_rt);
297 break;
298 case KVM_INST_MFSPR_SRR0:
299 kvm_patch_ins_ld(inst, magic_var(srr0), inst_rt);
300 break;
301 case KVM_INST_MFSPR_SRR1:
302 kvm_patch_ins_ld(inst, magic_var(srr1), inst_rt);
303 break;
304 case KVM_INST_MFSPR_DAR:
305 kvm_patch_ins_ld(inst, magic_var(dar), inst_rt);
306 break;
307 case KVM_INST_MFSPR_DSISR:
308 kvm_patch_ins_lwz(inst, magic_var(dsisr), inst_rt);
309 break;
310
311 /* Stores */
312 case KVM_INST_MTSPR_SPRG0:
313 kvm_patch_ins_std(inst, magic_var(sprg0), inst_rt);
314 break;
315 case KVM_INST_MTSPR_SPRG1:
316 kvm_patch_ins_std(inst, magic_var(sprg1), inst_rt);
317 break;
318 case KVM_INST_MTSPR_SPRG2:
319 kvm_patch_ins_std(inst, magic_var(sprg2), inst_rt);
320 break;
321 case KVM_INST_MTSPR_SPRG3:
322 kvm_patch_ins_std(inst, magic_var(sprg3), inst_rt);
323 break;
324 case KVM_INST_MTSPR_SRR0:
325 kvm_patch_ins_std(inst, magic_var(srr0), inst_rt);
326 break;
327 case KVM_INST_MTSPR_SRR1:
328 kvm_patch_ins_std(inst, magic_var(srr1), inst_rt);
329 break;
330 case KVM_INST_MTSPR_DAR:
331 kvm_patch_ins_std(inst, magic_var(dar), inst_rt);
332 break;
333 case KVM_INST_MTSPR_DSISR:
334 kvm_patch_ins_stw(inst, magic_var(dsisr), inst_rt);
335 break;
Alexander Grafd1290b152010-07-29 14:48:00 +0200336
337 /* Nops */
338 case KVM_INST_TLBSYNC:
339 kvm_patch_ins_nop(inst);
340 break;
Alexander Graf819a63d2010-07-29 14:48:04 +0200341
342 /* Rewrites */
343 case KVM_INST_MTMSRD_L1:
344 /* We use r30 and r31 during the hook */
345 if (get_rt(inst_rt) < 30)
346 kvm_patch_ins_mtmsrd(inst, inst_rt);
347 break;
Alexander Graf78109272010-07-29 14:48:05 +0200348 case KVM_INST_MTMSR:
349 case KVM_INST_MTMSRD_L0:
350 /* We use r30 and r31 during the hook */
351 if (get_rt(inst_rt) < 30)
352 kvm_patch_ins_mtmsr(inst, inst_rt);
353 break;
Alexander Graf73a18102010-07-29 14:47:58 +0200354 }
355
356 switch (_inst) {
Alexander Graf644bfa02010-07-29 14:48:06 +0200357#ifdef CONFIG_BOOKE
358 case KVM_INST_WRTEEI_0:
359 case KVM_INST_WRTEEI_1:
360 kvm_patch_ins_wrteei(inst);
361 break;
362#endif
Alexander Graf73a18102010-07-29 14:47:58 +0200363 }
364}
365
366static void kvm_use_magic_page(void)
367{
368 u32 *p;
369 u32 *start, *end;
370 u32 tmp;
371
372 /* Tell the host to map the magic page to -4096 on all CPUs */
373 on_each_cpu(kvm_map_magic_page, NULL, 1);
374
375 /* Quick self-test to see if the mapping works */
376 if (__get_user(tmp, (u32*)KVM_MAGIC_PAGE)) {
377 kvm_patching_worked = false;
378 return;
379 }
380
381 /* Now loop through all code and find instructions */
382 start = (void*)_stext;
383 end = (void*)_etext;
384
385 for (p = start; p < end; p++)
386 kvm_check_ins(p);
387
388 printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
389 kvm_patching_worked ? "worked" : "failed");
390}
391
Alexander Graf2a342ed2010-07-29 14:47:48 +0200392unsigned long kvm_hypercall(unsigned long *in,
393 unsigned long *out,
394 unsigned long nr)
395{
396 unsigned long register r0 asm("r0");
397 unsigned long register r3 asm("r3") = in[0];
398 unsigned long register r4 asm("r4") = in[1];
399 unsigned long register r5 asm("r5") = in[2];
400 unsigned long register r6 asm("r6") = in[3];
401 unsigned long register r7 asm("r7") = in[4];
402 unsigned long register r8 asm("r8") = in[5];
403 unsigned long register r9 asm("r9") = in[6];
404 unsigned long register r10 asm("r10") = in[7];
405 unsigned long register r11 asm("r11") = nr;
406 unsigned long register r12 asm("r12");
407
408 asm volatile("bl kvm_hypercall_start"
409 : "=r"(r0), "=r"(r3), "=r"(r4), "=r"(r5), "=r"(r6),
410 "=r"(r7), "=r"(r8), "=r"(r9), "=r"(r10), "=r"(r11),
411 "=r"(r12)
412 : "r"(r3), "r"(r4), "r"(r5), "r"(r6), "r"(r7), "r"(r8),
413 "r"(r9), "r"(r10), "r"(r11)
414 : "memory", "cc", "xer", "ctr", "lr");
415
416 out[0] = r4;
417 out[1] = r5;
418 out[2] = r6;
419 out[3] = r7;
420 out[4] = r8;
421 out[5] = r9;
422 out[6] = r10;
423 out[7] = r11;
424
425 return r3;
426}
427EXPORT_SYMBOL_GPL(kvm_hypercall);
Alexander Graf73a18102010-07-29 14:47:58 +0200428
429static int kvm_para_setup(void)
430{
431 extern u32 kvm_hypercall_start;
432 struct device_node *hyper_node;
433 u32 *insts;
434 int len, i;
435
436 hyper_node = of_find_node_by_path("/hypervisor");
437 if (!hyper_node)
438 return -1;
439
440 insts = (u32*)of_get_property(hyper_node, "hcall-instructions", &len);
441 if (len % 4)
442 return -1;
443 if (len > (4 * 4))
444 return -1;
445
446 for (i = 0; i < (len / 4); i++)
447 kvm_patch_ins(&(&kvm_hypercall_start)[i], insts[i]);
448
449 return 0;
450}
451
Alexander Graf2d4f5672010-07-29 14:48:01 +0200452static __init void kvm_free_tmp(void)
453{
454 unsigned long start, end;
455
456 start = (ulong)&kvm_tmp[kvm_tmp_index + (PAGE_SIZE - 1)] & PAGE_MASK;
457 end = (ulong)&kvm_tmp[ARRAY_SIZE(kvm_tmp)] & PAGE_MASK;
458
459 /* Free the tmp space we don't need */
460 for (; start < end; start += PAGE_SIZE) {
461 ClearPageReserved(virt_to_page(start));
462 init_page_count(virt_to_page(start));
463 free_page(start);
464 totalram_pages++;
465 }
466}
467
Alexander Graf73a18102010-07-29 14:47:58 +0200468static int __init kvm_guest_init(void)
469{
470 if (!kvm_para_available())
Alexander Graf2d4f5672010-07-29 14:48:01 +0200471 goto free_tmp;
Alexander Graf73a18102010-07-29 14:47:58 +0200472
473 if (kvm_para_setup())
Alexander Graf2d4f5672010-07-29 14:48:01 +0200474 goto free_tmp;
Alexander Graf73a18102010-07-29 14:47:58 +0200475
476 if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
477 kvm_use_magic_page();
478
Alexander Graf2d4f5672010-07-29 14:48:01 +0200479free_tmp:
480 kvm_free_tmp();
481
Alexander Graf73a18102010-07-29 14:47:58 +0200482 return 0;
483}
484
485postcore_initcall(kvm_guest_init);