Skip to content

Commit 2d4f567

Browse files
agrafavikivity
authored andcommitted
KVM: PPC: Introduce kvm_tmp framework
We will soon require more sophisticated methods to replace single instructions with multiple instructions. We do that by branching to a memory region where we write replacement code for the instruction to. This region needs to be within 32 MB of the patched instruction though, because that's the furthest we can jump with immediate branches. So we keep 1MB of free space around in bss. After we're done initing we can just tell the mm system that the unused pages are free, but until then we have enough space to fit all our code in. Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent d1290b1 commit 2d4f567

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

‎arch/powerpc/kernel/kvm.c‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
#define KVM_INST_TLBSYNC 0x7c00046c
6666

6767
static bool kvm_patching_worked = true;
68+
static char kvm_tmp[1024 * 1024];
69+
static int kvm_tmp_index;
6870

6971
static inline void kvm_patch_ins(u32 *inst, u32 new_inst)
7072
{
@@ -105,6 +107,23 @@ static void kvm_patch_ins_nop(u32 *inst)
105107
kvm_patch_ins(inst, KVM_INST_NOP);
106108
}
107109

110+
static u32 *kvm_alloc(int len)
111+
{
112+
u32 *p;
113+
114+
if ((kvm_tmp_index + len) > ARRAY_SIZE(kvm_tmp)) {
115+
printk(KERN_ERR "KVM: No more space (%d + %d)\n",
116+
kvm_tmp_index, len);
117+
kvm_patching_worked = false;
118+
return NULL;
119+
}
120+
121+
p = (void*)&kvm_tmp[kvm_tmp_index];
122+
kvm_tmp_index += len;
123+
124+
return p;
125+
}
126+
108127
static void kvm_map_magic_page(void *data)
109128
{
110129
kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -270,17 +289,36 @@ static int kvm_para_setup(void)
270289
return 0;
271290
}
272291

292+
static __init void kvm_free_tmp(void)
293+
{
294+
unsigned long start, end;
295+
296+
start = (ulong)&kvm_tmp[kvm_tmp_index + (PAGE_SIZE - 1)] & PAGE_MASK;
297+
end = (ulong)&kvm_tmp[ARRAY_SIZE(kvm_tmp)] & PAGE_MASK;
298+
299+
/* Free the tmp space we don't need */
300+
for (; start < end; start += PAGE_SIZE) {
301+
ClearPageReserved(virt_to_page(start));
302+
init_page_count(virt_to_page(start));
303+
free_page(start);
304+
totalram_pages++;
305+
}
306+
}
307+
273308
static int __init kvm_guest_init(void)
274309
{
275310
if (!kvm_para_available())
276-
return 0;
311+
goto free_tmp;
277312

278313
if (kvm_para_setup())
279-
return 0;
314+
goto free_tmp;
280315

281316
if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
282317
kvm_use_magic_page();
283318

319+
free_tmp:
320+
kvm_free_tmp();
321+
284322
return 0;
285323
}
286324

0 commit comments

Comments
 (0)