blob: 683b5b3805bd17493d97c261afc19279ac76b69f [file] [log] [blame]
Alexander Graf2a342ed2010-07-29 14:47:48 +02001/*
2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
Scott Wood940b45e2011-11-08 18:23:28 -06003 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Alexander Graf2a342ed2010-07-29 14:47:48 +02004 *
5 * Authors:
6 * Alexander Graf <agraf@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, version 2, as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include <linux/kvm_host.h>
23#include <linux/init.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040024#include <linux/export.h>
Alexander Graf2a342ed2010-07-29 14:47:48 +020025#include <linux/kvm_para.h>
26#include <linux/slab.h>
27#include <linux/of.h>
Mathieu Malaterre9f9eae52018-03-28 21:58:11 +020028#include <linux/pagemap.h>
Alexander Graf2a342ed2010-07-29 14:47:48 +020029
30#include <asm/reg.h>
Alexander Graf2a342ed2010-07-29 14:47:48 +020031#include <asm/sections.h>
32#include <asm/cacheflush.h>
33#include <asm/disassemble.h>
Scott Wood940b45e2011-11-08 18:23:28 -060034#include <asm/ppc-opcode.h>
Liu Yu-B132012e1ae9c02012-03-15 10:52:13 +000035#include <asm/epapr_hcalls.h>
Alexander Graf2a342ed2010-07-29 14:47:48 +020036
Alexander Grafd17051c2010-07-29 14:47:57 +020037#define KVM_MAGIC_PAGE (-4096L)
38#define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
39
Alexander Grafd1293c92010-07-29 14:47:59 +020040#define KVM_INST_LWZ 0x80000000
41#define KVM_INST_STW 0x90000000
42#define KVM_INST_LD 0xe8000000
43#define KVM_INST_STD 0xf8000000
44#define KVM_INST_NOP 0x60000000
45#define KVM_INST_B 0x48000000
46#define KVM_INST_B_MASK 0x03ffffff
47#define KVM_INST_B_MAX 0x01ffffff
Scott Wood940b45e2011-11-08 18:23:28 -060048#define KVM_INST_LI 0x38000000
Alexander Grafd1293c92010-07-29 14:47:59 +020049
Alexander Graf73a18102010-07-29 14:47:58 +020050#define KVM_MASK_RT 0x03e00000
Alexander Graf512ba592010-08-05 11:26:04 +020051#define KVM_RT_30 0x03c00000
Alexander Grafcbe487f2010-08-03 10:39:35 +020052#define KVM_MASK_RB 0x0000f800
Alexander Grafd1293c92010-07-29 14:47:59 +020053#define KVM_INST_MFMSR 0x7c0000a6
Alexander Grafd1293c92010-07-29 14:47:59 +020054
Scott Woodb5904972011-11-08 18:23:30 -060055#define SPR_FROM 0
56#define SPR_TO 0x100
57
58#define KVM_INST_SPR(sprn, moveto) (0x7c0002a6 | \
59 (((sprn) & 0x1f) << 16) | \
60 (((sprn) & 0x3e0) << 6) | \
61 (moveto))
62
63#define KVM_INST_MFSPR(sprn) KVM_INST_SPR(sprn, SPR_FROM)
64#define KVM_INST_MTSPR(sprn) KVM_INST_SPR(sprn, SPR_TO)
Alexander Graf73a18102010-07-29 14:47:58 +020065
Alexander Grafd1290b152010-07-29 14:48:00 +020066#define KVM_INST_TLBSYNC 0x7c00046c
Alexander Graf78109272010-07-29 14:48:05 +020067#define KVM_INST_MTMSRD_L0 0x7c000164
Alexander Graf819a63d2010-07-29 14:48:04 +020068#define KVM_INST_MTMSRD_L1 0x7c010164
Alexander Graf78109272010-07-29 14:48:05 +020069#define KVM_INST_MTMSR 0x7c000124
Alexander Grafd1290b152010-07-29 14:48:00 +020070
Scott Wood940b45e2011-11-08 18:23:28 -060071#define KVM_INST_WRTEE 0x7c000106
Alexander Graf644bfa02010-07-29 14:48:06 +020072#define KVM_INST_WRTEEI_0 0x7c000146
73#define KVM_INST_WRTEEI_1 0x7c008146
74
Alexander Grafcbe487f2010-08-03 10:39:35 +020075#define KVM_INST_MTSRIN 0x7c0001e4
76
Alexander Graf73a18102010-07-29 14:47:58 +020077static bool kvm_patching_worked = true;
Alexander Grafb18db0b2014-04-29 12:17:26 +020078char kvm_tmp[1024 * 1024];
Alexander Graf2d4f5672010-07-29 14:48:01 +020079static int kvm_tmp_index;
Alexander Graf73a18102010-07-29 14:47:58 +020080
81static inline void kvm_patch_ins(u32 *inst, u32 new_inst)
82{
83 *inst = new_inst;
84 flush_icache_range((ulong)inst, (ulong)inst + 4);
85}
86
Alexander Graf512ba592010-08-05 11:26:04 +020087static void kvm_patch_ins_ll(u32 *inst, long addr, u32 rt)
88{
89#ifdef CONFIG_64BIT
90 kvm_patch_ins(inst, KVM_INST_LD | rt | (addr & 0x0000fffc));
91#else
92 kvm_patch_ins(inst, KVM_INST_LWZ | rt | (addr & 0x0000fffc));
93#endif
94}
95
Alexander Grafd1293c92010-07-29 14:47:59 +020096static void kvm_patch_ins_ld(u32 *inst, long addr, u32 rt)
97{
98#ifdef CONFIG_64BIT
99 kvm_patch_ins(inst, KVM_INST_LD | rt | (addr & 0x0000fffc));
100#else
101 kvm_patch_ins(inst, KVM_INST_LWZ | rt | ((addr + 4) & 0x0000fffc));
102#endif
103}
104
105static void kvm_patch_ins_lwz(u32 *inst, long addr, u32 rt)
106{
107 kvm_patch_ins(inst, KVM_INST_LWZ | rt | (addr & 0x0000ffff));
108}
109
110static void kvm_patch_ins_std(u32 *inst, long addr, u32 rt)
111{
112#ifdef CONFIG_64BIT
113 kvm_patch_ins(inst, KVM_INST_STD | rt | (addr & 0x0000fffc));
114#else
115 kvm_patch_ins(inst, KVM_INST_STW | rt | ((addr + 4) & 0x0000fffc));
116#endif
117}
118
119static void kvm_patch_ins_stw(u32 *inst, long addr, u32 rt)
120{
121 kvm_patch_ins(inst, KVM_INST_STW | rt | (addr & 0x0000fffc));
122}
123
Alexander Grafd1290b152010-07-29 14:48:00 +0200124static void kvm_patch_ins_nop(u32 *inst)
125{
126 kvm_patch_ins(inst, KVM_INST_NOP);
127}
128
Alexander Graf71ee8e32010-07-29 14:48:02 +0200129static void kvm_patch_ins_b(u32 *inst, int addr)
130{
Scott Wooda36be102010-10-18 17:35:48 -0500131#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_PPC_BOOK3S)
Alexander Graf71ee8e32010-07-29 14:48:02 +0200132 /* On relocatable kernels interrupts handlers and our code
133 can be in different regions, so we don't patch them */
134
Alexander Graf71ee8e32010-07-29 14:48:02 +0200135 if ((ulong)inst < (ulong)&__end_interrupts)
136 return;
137#endif
138
139 kvm_patch_ins(inst, KVM_INST_B | (addr & KVM_INST_B_MASK));
140}
141
Alexander Graf2d4f5672010-07-29 14:48:01 +0200142static u32 *kvm_alloc(int len)
143{
144 u32 *p;
145
146 if ((kvm_tmp_index + len) > ARRAY_SIZE(kvm_tmp)) {
147 printk(KERN_ERR "KVM: No more space (%d + %d)\n",
148 kvm_tmp_index, len);
149 kvm_patching_worked = false;
150 return NULL;
151 }
152
153 p = (void*)&kvm_tmp[kvm_tmp_index];
154 kvm_tmp_index += len;
155
156 return p;
157}
158
Alexander Graf819a63d2010-07-29 14:48:04 +0200159extern u32 kvm_emulate_mtmsrd_branch_offs;
160extern u32 kvm_emulate_mtmsrd_reg_offs;
Alexander Grafdf08bd12010-08-05 15:44:41 +0200161extern u32 kvm_emulate_mtmsrd_orig_ins_offs;
Alexander Graf819a63d2010-07-29 14:48:04 +0200162extern u32 kvm_emulate_mtmsrd_len;
163extern u32 kvm_emulate_mtmsrd[];
164
165static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)
166{
167 u32 *p;
168 int distance_start;
169 int distance_end;
170 ulong next_inst;
171
172 p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
173 if (!p)
174 return;
175
176 /* Find out where we are and put everything there */
177 distance_start = (ulong)p - (ulong)inst;
178 next_inst = ((ulong)inst + 4);
179 distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs];
180
181 /* Make sure we only write valid b instructions */
182 if (distance_start > KVM_INST_B_MAX) {
183 kvm_patching_worked = false;
184 return;
185 }
186
187 /* Modify the chunk to fit the invocation */
188 memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
189 p[kvm_emulate_mtmsrd_branch_offs] |= distance_end & KVM_INST_B_MASK;
Alexander Grafdf08bd12010-08-05 15:44:41 +0200190 switch (get_rt(rt)) {
191 case 30:
192 kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
193 magic_var(scratch2), KVM_RT_30);
194 break;
195 case 31:
196 kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
197 magic_var(scratch1), KVM_RT_30);
198 break;
199 default:
200 p[kvm_emulate_mtmsrd_reg_offs] |= rt;
201 break;
202 }
203
204 p[kvm_emulate_mtmsrd_orig_ins_offs] = *inst;
Alexander Graf819a63d2010-07-29 14:48:04 +0200205 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsrd_len * 4);
206
207 /* Patch the invocation */
208 kvm_patch_ins_b(inst, distance_start);
209}
210
Alexander Graf78109272010-07-29 14:48:05 +0200211extern u32 kvm_emulate_mtmsr_branch_offs;
212extern u32 kvm_emulate_mtmsr_reg1_offs;
213extern u32 kvm_emulate_mtmsr_reg2_offs;
Alexander Graf78109272010-07-29 14:48:05 +0200214extern u32 kvm_emulate_mtmsr_orig_ins_offs;
215extern u32 kvm_emulate_mtmsr_len;
216extern u32 kvm_emulate_mtmsr[];
217
218static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)
219{
220 u32 *p;
221 int distance_start;
222 int distance_end;
223 ulong next_inst;
224
225 p = kvm_alloc(kvm_emulate_mtmsr_len * 4);
226 if (!p)
227 return;
228
229 /* Find out where we are and put everything there */
230 distance_start = (ulong)p - (ulong)inst;
231 next_inst = ((ulong)inst + 4);
232 distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsr_branch_offs];
233
234 /* Make sure we only write valid b instructions */
235 if (distance_start > KVM_INST_B_MAX) {
236 kvm_patching_worked = false;
237 return;
238 }
239
240 /* Modify the chunk to fit the invocation */
241 memcpy(p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len * 4);
242 p[kvm_emulate_mtmsr_branch_offs] |= distance_end & KVM_INST_B_MASK;
Alexander Graf512ba592010-08-05 11:26:04 +0200243
244 /* Make clobbered registers work too */
245 switch (get_rt(rt)) {
246 case 30:
247 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
248 magic_var(scratch2), KVM_RT_30);
249 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
250 magic_var(scratch2), KVM_RT_30);
251 break;
252 case 31:
253 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
254 magic_var(scratch1), KVM_RT_30);
255 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
256 magic_var(scratch1), KVM_RT_30);
257 break;
258 default:
259 p[kvm_emulate_mtmsr_reg1_offs] |= rt;
260 p[kvm_emulate_mtmsr_reg2_offs] |= rt;
261 break;
262 }
263
Alexander Graf78109272010-07-29 14:48:05 +0200264 p[kvm_emulate_mtmsr_orig_ins_offs] = *inst;
265 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsr_len * 4);
266
267 /* Patch the invocation */
268 kvm_patch_ins_b(inst, distance_start);
269}
270
Alexander Graf644bfa02010-07-29 14:48:06 +0200271#ifdef CONFIG_BOOKE
272
Scott Wood940b45e2011-11-08 18:23:28 -0600273extern u32 kvm_emulate_wrtee_branch_offs;
274extern u32 kvm_emulate_wrtee_reg_offs;
275extern u32 kvm_emulate_wrtee_orig_ins_offs;
276extern u32 kvm_emulate_wrtee_len;
277extern u32 kvm_emulate_wrtee[];
Alexander Graf644bfa02010-07-29 14:48:06 +0200278
Scott Wood940b45e2011-11-08 18:23:28 -0600279static void kvm_patch_ins_wrtee(u32 *inst, u32 rt, int imm_one)
Alexander Graf644bfa02010-07-29 14:48:06 +0200280{
281 u32 *p;
282 int distance_start;
283 int distance_end;
284 ulong next_inst;
285
Scott Wood940b45e2011-11-08 18:23:28 -0600286 p = kvm_alloc(kvm_emulate_wrtee_len * 4);
Alexander Graf644bfa02010-07-29 14:48:06 +0200287 if (!p)
288 return;
289
290 /* Find out where we are and put everything there */
291 distance_start = (ulong)p - (ulong)inst;
292 next_inst = ((ulong)inst + 4);
Scott Wood940b45e2011-11-08 18:23:28 -0600293 distance_end = next_inst - (ulong)&p[kvm_emulate_wrtee_branch_offs];
Alexander Graf644bfa02010-07-29 14:48:06 +0200294
295 /* Make sure we only write valid b instructions */
296 if (distance_start > KVM_INST_B_MAX) {
297 kvm_patching_worked = false;
298 return;
299 }
300
301 /* Modify the chunk to fit the invocation */
Scott Wood940b45e2011-11-08 18:23:28 -0600302 memcpy(p, kvm_emulate_wrtee, kvm_emulate_wrtee_len * 4);
303 p[kvm_emulate_wrtee_branch_offs] |= distance_end & KVM_INST_B_MASK;
304
305 if (imm_one) {
306 p[kvm_emulate_wrtee_reg_offs] =
Michael Neulingc75df6f2012-06-25 13:33:10 +0000307 KVM_INST_LI | __PPC_RT(R30) | MSR_EE;
Scott Wood940b45e2011-11-08 18:23:28 -0600308 } else {
309 /* Make clobbered registers work too */
310 switch (get_rt(rt)) {
311 case 30:
312 kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
313 magic_var(scratch2), KVM_RT_30);
314 break;
315 case 31:
316 kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
317 magic_var(scratch1), KVM_RT_30);
318 break;
319 default:
320 p[kvm_emulate_wrtee_reg_offs] |= rt;
321 break;
322 }
323 }
324
325 p[kvm_emulate_wrtee_orig_ins_offs] = *inst;
326 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrtee_len * 4);
327
328 /* Patch the invocation */
329 kvm_patch_ins_b(inst, distance_start);
330}
331
332extern u32 kvm_emulate_wrteei_0_branch_offs;
333extern u32 kvm_emulate_wrteei_0_len;
334extern u32 kvm_emulate_wrteei_0[];
335
336static void kvm_patch_ins_wrteei_0(u32 *inst)
337{
338 u32 *p;
339 int distance_start;
340 int distance_end;
341 ulong next_inst;
342
343 p = kvm_alloc(kvm_emulate_wrteei_0_len * 4);
344 if (!p)
345 return;
346
347 /* Find out where we are and put everything there */
348 distance_start = (ulong)p - (ulong)inst;
349 next_inst = ((ulong)inst + 4);
350 distance_end = next_inst - (ulong)&p[kvm_emulate_wrteei_0_branch_offs];
351
352 /* Make sure we only write valid b instructions */
353 if (distance_start > KVM_INST_B_MAX) {
354 kvm_patching_worked = false;
355 return;
356 }
357
358 memcpy(p, kvm_emulate_wrteei_0, kvm_emulate_wrteei_0_len * 4);
359 p[kvm_emulate_wrteei_0_branch_offs] |= distance_end & KVM_INST_B_MASK;
360 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrteei_0_len * 4);
Alexander Graf644bfa02010-07-29 14:48:06 +0200361
362 /* Patch the invocation */
363 kvm_patch_ins_b(inst, distance_start);
364}
365
366#endif
367
Alexander Grafcbe487f2010-08-03 10:39:35 +0200368#ifdef CONFIG_PPC_BOOK3S_32
369
370extern u32 kvm_emulate_mtsrin_branch_offs;
371extern u32 kvm_emulate_mtsrin_reg1_offs;
372extern u32 kvm_emulate_mtsrin_reg2_offs;
373extern u32 kvm_emulate_mtsrin_orig_ins_offs;
374extern u32 kvm_emulate_mtsrin_len;
375extern u32 kvm_emulate_mtsrin[];
376
377static void kvm_patch_ins_mtsrin(u32 *inst, u32 rt, u32 rb)
378{
379 u32 *p;
380 int distance_start;
381 int distance_end;
382 ulong next_inst;
383
384 p = kvm_alloc(kvm_emulate_mtsrin_len * 4);
385 if (!p)
386 return;
387
388 /* Find out where we are and put everything there */
389 distance_start = (ulong)p - (ulong)inst;
390 next_inst = ((ulong)inst + 4);
391 distance_end = next_inst - (ulong)&p[kvm_emulate_mtsrin_branch_offs];
392
393 /* Make sure we only write valid b instructions */
394 if (distance_start > KVM_INST_B_MAX) {
395 kvm_patching_worked = false;
396 return;
397 }
398
399 /* Modify the chunk to fit the invocation */
400 memcpy(p, kvm_emulate_mtsrin, kvm_emulate_mtsrin_len * 4);
401 p[kvm_emulate_mtsrin_branch_offs] |= distance_end & KVM_INST_B_MASK;
402 p[kvm_emulate_mtsrin_reg1_offs] |= (rb << 10);
403 p[kvm_emulate_mtsrin_reg2_offs] |= rt;
404 p[kvm_emulate_mtsrin_orig_ins_offs] = *inst;
405 flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtsrin_len * 4);
406
407 /* Patch the invocation */
408 kvm_patch_ins_b(inst, distance_start);
409}
410
411#endif
412
Alexander Graf73a18102010-07-29 14:47:58 +0200413static void kvm_map_magic_page(void *data)
414{
Alexander Graf7508e162010-08-03 11:32:56 +0200415 u32 *features = data;
416
Bharat Bhushan1820a8d2013-10-08 09:32:19 +0530417 ulong in[8] = {0};
Alexander Graf7508e162010-08-03 11:32:56 +0200418 ulong out[8];
419
420 in[0] = KVM_MAGIC_PAGE;
Alexander Graf5c165ae2014-05-12 01:11:55 +0200421 in[1] = KVM_MAGIC_PAGE | MAGIC_PAGE_FLAG_NOT_MAPPED_NX;
Alexander Graf7508e162010-08-03 11:32:56 +0200422
Bharat Bhushan1820a8d2013-10-08 09:32:19 +0530423 epapr_hypercall(in, out, KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE));
Alexander Graf7508e162010-08-03 11:32:56 +0200424
425 *features = out[0];
Alexander Graf73a18102010-07-29 14:47:58 +0200426}
427
Alexander Graf7508e162010-08-03 11:32:56 +0200428static void kvm_check_ins(u32 *inst, u32 features)
Alexander Graf73a18102010-07-29 14:47:58 +0200429{
430 u32 _inst = *inst;
431 u32 inst_no_rt = _inst & ~KVM_MASK_RT;
432 u32 inst_rt = _inst & KVM_MASK_RT;
433
434 switch (inst_no_rt) {
Alexander Grafd1293c92010-07-29 14:47:59 +0200435 /* Loads */
436 case KVM_INST_MFMSR:
437 kvm_patch_ins_ld(inst, magic_var(msr), inst_rt);
438 break;
Scott Woodb5904972011-11-08 18:23:30 -0600439 case KVM_INST_MFSPR(SPRN_SPRG0):
Alexander Grafd1293c92010-07-29 14:47:59 +0200440 kvm_patch_ins_ld(inst, magic_var(sprg0), inst_rt);
441 break;
Scott Woodb5904972011-11-08 18:23:30 -0600442 case KVM_INST_MFSPR(SPRN_SPRG1):
Alexander Grafd1293c92010-07-29 14:47:59 +0200443 kvm_patch_ins_ld(inst, magic_var(sprg1), inst_rt);
444 break;
Scott Woodb5904972011-11-08 18:23:30 -0600445 case KVM_INST_MFSPR(SPRN_SPRG2):
Alexander Grafd1293c92010-07-29 14:47:59 +0200446 kvm_patch_ins_ld(inst, magic_var(sprg2), inst_rt);
447 break;
Scott Woodb5904972011-11-08 18:23:30 -0600448 case KVM_INST_MFSPR(SPRN_SPRG3):
Alexander Grafd1293c92010-07-29 14:47:59 +0200449 kvm_patch_ins_ld(inst, magic_var(sprg3), inst_rt);
450 break;
Scott Woodb5904972011-11-08 18:23:30 -0600451 case KVM_INST_MFSPR(SPRN_SRR0):
Alexander Grafd1293c92010-07-29 14:47:59 +0200452 kvm_patch_ins_ld(inst, magic_var(srr0), inst_rt);
453 break;
Scott Woodb5904972011-11-08 18:23:30 -0600454 case KVM_INST_MFSPR(SPRN_SRR1):
Alexander Grafd1293c92010-07-29 14:47:59 +0200455 kvm_patch_ins_ld(inst, magic_var(srr1), inst_rt);
456 break;
Scott Woodb5904972011-11-08 18:23:30 -0600457#ifdef CONFIG_BOOKE
458 case KVM_INST_MFSPR(SPRN_DEAR):
459#else
460 case KVM_INST_MFSPR(SPRN_DAR):
461#endif
Alexander Grafd1293c92010-07-29 14:47:59 +0200462 kvm_patch_ins_ld(inst, magic_var(dar), inst_rt);
463 break;
Scott Woodb5904972011-11-08 18:23:30 -0600464 case KVM_INST_MFSPR(SPRN_DSISR):
Alexander Grafd1293c92010-07-29 14:47:59 +0200465 kvm_patch_ins_lwz(inst, magic_var(dsisr), inst_rt);
466 break;
467
Scott Woodb5904972011-11-08 18:23:30 -0600468#ifdef CONFIG_PPC_BOOK3E_MMU
469 case KVM_INST_MFSPR(SPRN_MAS0):
470 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
471 kvm_patch_ins_lwz(inst, magic_var(mas0), inst_rt);
472 break;
473 case KVM_INST_MFSPR(SPRN_MAS1):
474 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
475 kvm_patch_ins_lwz(inst, magic_var(mas1), inst_rt);
476 break;
477 case KVM_INST_MFSPR(SPRN_MAS2):
478 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
479 kvm_patch_ins_ld(inst, magic_var(mas2), inst_rt);
480 break;
481 case KVM_INST_MFSPR(SPRN_MAS3):
482 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
483 kvm_patch_ins_lwz(inst, magic_var(mas7_3) + 4, inst_rt);
484 break;
485 case KVM_INST_MFSPR(SPRN_MAS4):
486 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
487 kvm_patch_ins_lwz(inst, magic_var(mas4), inst_rt);
488 break;
489 case KVM_INST_MFSPR(SPRN_MAS6):
490 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
491 kvm_patch_ins_lwz(inst, magic_var(mas6), inst_rt);
492 break;
493 case KVM_INST_MFSPR(SPRN_MAS7):
494 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
495 kvm_patch_ins_lwz(inst, magic_var(mas7_3), inst_rt);
496 break;
497#endif /* CONFIG_PPC_BOOK3E_MMU */
498
499 case KVM_INST_MFSPR(SPRN_SPRG4):
500#ifdef CONFIG_BOOKE
501 case KVM_INST_MFSPR(SPRN_SPRG4R):
502#endif
503 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
504 kvm_patch_ins_ld(inst, magic_var(sprg4), inst_rt);
505 break;
506 case KVM_INST_MFSPR(SPRN_SPRG5):
507#ifdef CONFIG_BOOKE
508 case KVM_INST_MFSPR(SPRN_SPRG5R):
509#endif
510 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
511 kvm_patch_ins_ld(inst, magic_var(sprg5), inst_rt);
512 break;
513 case KVM_INST_MFSPR(SPRN_SPRG6):
514#ifdef CONFIG_BOOKE
515 case KVM_INST_MFSPR(SPRN_SPRG6R):
516#endif
517 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
518 kvm_patch_ins_ld(inst, magic_var(sprg6), inst_rt);
519 break;
520 case KVM_INST_MFSPR(SPRN_SPRG7):
521#ifdef CONFIG_BOOKE
522 case KVM_INST_MFSPR(SPRN_SPRG7R):
523#endif
524 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
525 kvm_patch_ins_ld(inst, magic_var(sprg7), inst_rt);
526 break;
527
528#ifdef CONFIG_BOOKE
529 case KVM_INST_MFSPR(SPRN_ESR):
530 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
531 kvm_patch_ins_lwz(inst, magic_var(esr), inst_rt);
532 break;
533#endif
534
535 case KVM_INST_MFSPR(SPRN_PIR):
536 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
537 kvm_patch_ins_lwz(inst, magic_var(pir), inst_rt);
538 break;
539
540
Alexander Grafd1293c92010-07-29 14:47:59 +0200541 /* Stores */
Scott Woodb5904972011-11-08 18:23:30 -0600542 case KVM_INST_MTSPR(SPRN_SPRG0):
Alexander Grafd1293c92010-07-29 14:47:59 +0200543 kvm_patch_ins_std(inst, magic_var(sprg0), inst_rt);
544 break;
Scott Woodb5904972011-11-08 18:23:30 -0600545 case KVM_INST_MTSPR(SPRN_SPRG1):
Alexander Grafd1293c92010-07-29 14:47:59 +0200546 kvm_patch_ins_std(inst, magic_var(sprg1), inst_rt);
547 break;
Scott Woodb5904972011-11-08 18:23:30 -0600548 case KVM_INST_MTSPR(SPRN_SPRG2):
Alexander Grafd1293c92010-07-29 14:47:59 +0200549 kvm_patch_ins_std(inst, magic_var(sprg2), inst_rt);
550 break;
Scott Woodb5904972011-11-08 18:23:30 -0600551 case KVM_INST_MTSPR(SPRN_SPRG3):
Alexander Grafd1293c92010-07-29 14:47:59 +0200552 kvm_patch_ins_std(inst, magic_var(sprg3), inst_rt);
553 break;
Scott Woodb5904972011-11-08 18:23:30 -0600554 case KVM_INST_MTSPR(SPRN_SRR0):
Alexander Grafd1293c92010-07-29 14:47:59 +0200555 kvm_patch_ins_std(inst, magic_var(srr0), inst_rt);
556 break;
Scott Woodb5904972011-11-08 18:23:30 -0600557 case KVM_INST_MTSPR(SPRN_SRR1):
Alexander Grafd1293c92010-07-29 14:47:59 +0200558 kvm_patch_ins_std(inst, magic_var(srr1), inst_rt);
559 break;
Scott Woodb5904972011-11-08 18:23:30 -0600560#ifdef CONFIG_BOOKE
561 case KVM_INST_MTSPR(SPRN_DEAR):
562#else
563 case KVM_INST_MTSPR(SPRN_DAR):
564#endif
Alexander Grafd1293c92010-07-29 14:47:59 +0200565 kvm_patch_ins_std(inst, magic_var(dar), inst_rt);
566 break;
Scott Woodb5904972011-11-08 18:23:30 -0600567 case KVM_INST_MTSPR(SPRN_DSISR):
Alexander Grafd1293c92010-07-29 14:47:59 +0200568 kvm_patch_ins_stw(inst, magic_var(dsisr), inst_rt);
569 break;
Scott Woodb5904972011-11-08 18:23:30 -0600570#ifdef CONFIG_PPC_BOOK3E_MMU
571 case KVM_INST_MTSPR(SPRN_MAS0):
572 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
573 kvm_patch_ins_stw(inst, magic_var(mas0), inst_rt);
574 break;
575 case KVM_INST_MTSPR(SPRN_MAS1):
576 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
577 kvm_patch_ins_stw(inst, magic_var(mas1), inst_rt);
578 break;
579 case KVM_INST_MTSPR(SPRN_MAS2):
580 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
581 kvm_patch_ins_std(inst, magic_var(mas2), inst_rt);
582 break;
583 case KVM_INST_MTSPR(SPRN_MAS3):
584 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
585 kvm_patch_ins_stw(inst, magic_var(mas7_3) + 4, inst_rt);
586 break;
587 case KVM_INST_MTSPR(SPRN_MAS4):
588 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
589 kvm_patch_ins_stw(inst, magic_var(mas4), inst_rt);
590 break;
591 case KVM_INST_MTSPR(SPRN_MAS6):
592 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
593 kvm_patch_ins_stw(inst, magic_var(mas6), inst_rt);
594 break;
595 case KVM_INST_MTSPR(SPRN_MAS7):
596 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
597 kvm_patch_ins_stw(inst, magic_var(mas7_3), inst_rt);
598 break;
599#endif /* CONFIG_PPC_BOOK3E_MMU */
600
601 case KVM_INST_MTSPR(SPRN_SPRG4):
602 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
603 kvm_patch_ins_std(inst, magic_var(sprg4), inst_rt);
604 break;
605 case KVM_INST_MTSPR(SPRN_SPRG5):
606 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
607 kvm_patch_ins_std(inst, magic_var(sprg5), inst_rt);
608 break;
609 case KVM_INST_MTSPR(SPRN_SPRG6):
610 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
611 kvm_patch_ins_std(inst, magic_var(sprg6), inst_rt);
612 break;
613 case KVM_INST_MTSPR(SPRN_SPRG7):
614 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
615 kvm_patch_ins_std(inst, magic_var(sprg7), inst_rt);
616 break;
617
618#ifdef CONFIG_BOOKE
619 case KVM_INST_MTSPR(SPRN_ESR):
620 if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
621 kvm_patch_ins_stw(inst, magic_var(esr), inst_rt);
622 break;
623#endif
Alexander Grafd1290b152010-07-29 14:48:00 +0200624
625 /* Nops */
626 case KVM_INST_TLBSYNC:
627 kvm_patch_ins_nop(inst);
628 break;
Alexander Graf819a63d2010-07-29 14:48:04 +0200629
630 /* Rewrites */
631 case KVM_INST_MTMSRD_L1:
Alexander Grafdf08bd12010-08-05 15:44:41 +0200632 kvm_patch_ins_mtmsrd(inst, inst_rt);
Alexander Graf819a63d2010-07-29 14:48:04 +0200633 break;
Alexander Graf78109272010-07-29 14:48:05 +0200634 case KVM_INST_MTMSR:
635 case KVM_INST_MTMSRD_L0:
Alexander Graf512ba592010-08-05 11:26:04 +0200636 kvm_patch_ins_mtmsr(inst, inst_rt);
Alexander Graf78109272010-07-29 14:48:05 +0200637 break;
Scott Wood940b45e2011-11-08 18:23:28 -0600638#ifdef CONFIG_BOOKE
639 case KVM_INST_WRTEE:
640 kvm_patch_ins_wrtee(inst, inst_rt, 0);
641 break;
642#endif
Alexander Graf73a18102010-07-29 14:47:58 +0200643 }
644
Alexander Grafcbe487f2010-08-03 10:39:35 +0200645 switch (inst_no_rt & ~KVM_MASK_RB) {
646#ifdef CONFIG_PPC_BOOK3S_32
647 case KVM_INST_MTSRIN:
648 if (features & KVM_MAGIC_FEAT_SR) {
649 u32 inst_rb = _inst & KVM_MASK_RB;
650 kvm_patch_ins_mtsrin(inst, inst_rt, inst_rb);
651 }
652 break;
Alexander Grafcbe487f2010-08-03 10:39:35 +0200653#endif
654 }
655
Alexander Graf73a18102010-07-29 14:47:58 +0200656 switch (_inst) {
Alexander Graf644bfa02010-07-29 14:48:06 +0200657#ifdef CONFIG_BOOKE
658 case KVM_INST_WRTEEI_0:
Scott Wood940b45e2011-11-08 18:23:28 -0600659 kvm_patch_ins_wrteei_0(inst);
660 break;
661
Alexander Graf644bfa02010-07-29 14:48:06 +0200662 case KVM_INST_WRTEEI_1:
Scott Wood940b45e2011-11-08 18:23:28 -0600663 kvm_patch_ins_wrtee(inst, 0, 1);
Alexander Graf644bfa02010-07-29 14:48:06 +0200664 break;
665#endif
Alexander Graf73a18102010-07-29 14:47:58 +0200666 }
667}
668
Liu Yu-B13201befdc0a2011-12-01 20:22:53 +0000669extern u32 kvm_template_start[];
670extern u32 kvm_template_end[];
671
Alexander Graf73a18102010-07-29 14:47:58 +0200672static void kvm_use_magic_page(void)
673{
674 u32 *p;
675 u32 *start, *end;
Alexander Graf7508e162010-08-03 11:32:56 +0200676 u32 features;
Alexander Graf73a18102010-07-29 14:47:58 +0200677
678 /* Tell the host to map the magic page to -4096 on all CPUs */
Alexander Graf7508e162010-08-03 11:32:56 +0200679 on_each_cpu(kvm_map_magic_page, &features, 1);
Alexander Graf73a18102010-07-29 14:47:58 +0200680
681 /* Quick self-test to see if the mapping works */
Mathieu Malaterre9f9eae52018-03-28 21:58:11 +0200682 if (!fault_in_pages_readable((const char *)KVM_MAGIC_PAGE, sizeof(u32))) {
Alexander Graf73a18102010-07-29 14:47:58 +0200683 kvm_patching_worked = false;
684 return;
685 }
686
687 /* Now loop through all code and find instructions */
688 start = (void*)_stext;
689 end = (void*)_etext;
690
Scott Woodb5904972011-11-08 18:23:30 -0600691 /*
692 * Being interrupted in the middle of patching would
693 * be bad for SPRG4-7, which KVM can't keep in sync
694 * with emulated accesses because reads don't trap.
695 */
696 local_irq_disable();
697
Liu Yu-B13201befdc0a2011-12-01 20:22:53 +0000698 for (p = start; p < end; p++) {
699 /* Avoid patching the template code */
700 if (p >= kvm_template_start && p < kvm_template_end) {
701 p = kvm_template_end - 1;
702 continue;
703 }
Alexander Graf7508e162010-08-03 11:32:56 +0200704 kvm_check_ins(p, features);
Liu Yu-B13201befdc0a2011-12-01 20:22:53 +0000705 }
Alexander Graf73a18102010-07-29 14:47:58 +0200706
Scott Woodb5904972011-11-08 18:23:30 -0600707 local_irq_enable();
708
Alexander Graf73a18102010-07-29 14:47:58 +0200709 printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
710 kvm_patching_worked ? "worked" : "failed");
711}
712
Alexander Graf2d4f5672010-07-29 14:48:01 +0200713static __init void kvm_free_tmp(void)
714{
Jiang Liudbe67df2013-07-03 15:02:51 -0700715 free_reserved_area(&kvm_tmp[kvm_tmp_index],
716 &kvm_tmp[ARRAY_SIZE(kvm_tmp)], -1, NULL);
Alexander Graf2d4f5672010-07-29 14:48:01 +0200717}
718
Alexander Graf73a18102010-07-29 14:47:58 +0200719static int __init kvm_guest_init(void)
720{
721 if (!kvm_para_available())
Alexander Graf2d4f5672010-07-29 14:48:01 +0200722 goto free_tmp;
Alexander Graf73a18102010-07-29 14:47:58 +0200723
Liu Yu-B132012e1ae9c02012-03-15 10:52:13 +0000724 if (!epapr_paravirt_enabled)
Alexander Graf2d4f5672010-07-29 14:48:01 +0200725 goto free_tmp;
Alexander Graf73a18102010-07-29 14:47:58 +0200726
727 if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
728 kvm_use_magic_page();
729
Alexander Graf591bd8e2010-08-17 22:08:39 +0200730#ifdef CONFIG_PPC_BOOK3S_64
Alexander Grafad087372010-08-17 11:41:44 +0200731 /* Enable napping */
732 powersave_nap = 1;
Alexander Graf591bd8e2010-08-17 22:08:39 +0200733#endif
Alexander Grafad087372010-08-17 11:41:44 +0200734
Alexander Graf2d4f5672010-07-29 14:48:01 +0200735free_tmp:
736 kvm_free_tmp();
737
Alexander Graf73a18102010-07-29 14:47:58 +0200738 return 0;
739}
740
741postcore_initcall(kvm_guest_init);