2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
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.
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.
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.
21 #include <linux/kvm_host.h>
22 #include <linux/init.h>
23 #include <linux/kvm_para.h>
24 #include <linux/slab.h>
28 #include <asm/kvm_ppc.h>
29 #include <asm/sections.h>
30 #include <asm/cacheflush.h>
31 #include <asm/disassemble.h>
33 #define KVM_MAGIC_PAGE (-4096L)
34 #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
36 #define KVM_MASK_RT 0x03e00000
38 static bool kvm_patching_worked
= true;
40 static inline void kvm_patch_ins(u32
*inst
, u32 new_inst
)
43 flush_icache_range((ulong
)inst
, (ulong
)inst
+ 4);
46 static void kvm_map_magic_page(void *data
)
48 kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE
,
49 KVM_MAGIC_PAGE
, /* Physical Address */
50 KVM_MAGIC_PAGE
); /* Effective Address */
53 static void kvm_check_ins(u32
*inst
)
56 u32 inst_no_rt
= _inst
& ~KVM_MASK_RT
;
57 u32 inst_rt
= _inst
& KVM_MASK_RT
;
66 static void kvm_use_magic_page(void)
72 /* Tell the host to map the magic page to -4096 on all CPUs */
73 on_each_cpu(kvm_map_magic_page
, NULL
, 1);
75 /* Quick self-test to see if the mapping works */
76 if (__get_user(tmp
, (u32
*)KVM_MAGIC_PAGE
)) {
77 kvm_patching_worked
= false;
81 /* Now loop through all code and find instructions */
82 start
= (void*)_stext
;
85 for (p
= start
; p
< end
; p
++)
88 printk(KERN_INFO
"KVM: Live patching for a fast VM %s\n",
89 kvm_patching_worked
? "worked" : "failed");
92 unsigned long kvm_hypercall(unsigned long *in
,
96 unsigned long register r0
asm("r0");
97 unsigned long register r3
asm("r3") = in
[0];
98 unsigned long register r4
asm("r4") = in
[1];
99 unsigned long register r5
asm("r5") = in
[2];
100 unsigned long register r6
asm("r6") = in
[3];
101 unsigned long register r7
asm("r7") = in
[4];
102 unsigned long register r8
asm("r8") = in
[5];
103 unsigned long register r9
asm("r9") = in
[6];
104 unsigned long register r10
asm("r10") = in
[7];
105 unsigned long register r11
asm("r11") = nr
;
106 unsigned long register r12
asm("r12");
108 asm volatile("bl kvm_hypercall_start"
109 : "=r"(r0
), "=r"(r3
), "=r"(r4
), "=r"(r5
), "=r"(r6
),
110 "=r"(r7
), "=r"(r8
), "=r"(r9
), "=r"(r10
), "=r"(r11
),
112 : "r"(r3
), "r"(r4
), "r"(r5
), "r"(r6
), "r"(r7
), "r"(r8
),
113 "r"(r9
), "r"(r10
), "r"(r11
)
114 : "memory", "cc", "xer", "ctr", "lr");
127 EXPORT_SYMBOL_GPL(kvm_hypercall
);
129 static int kvm_para_setup(void)
131 extern u32 kvm_hypercall_start
;
132 struct device_node
*hyper_node
;
136 hyper_node
= of_find_node_by_path("/hypervisor");
140 insts
= (u32
*)of_get_property(hyper_node
, "hcall-instructions", &len
);
146 for (i
= 0; i
< (len
/ 4); i
++)
147 kvm_patch_ins(&(&kvm_hypercall_start
)[i
], insts
[i
]);
152 static int __init
kvm_guest_init(void)
154 if (!kvm_para_available())
157 if (kvm_para_setup())
160 if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE
))
161 kvm_use_magic_page();
166 postcore_initcall(kvm_guest_init
);