Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 1 | /* |
| 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 Graf | d17051c | 2010-07-29 14:47:57 +0200 | [diff] [blame] | 33 | #define KVM_MAGIC_PAGE (-4096L) |
| 34 | #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x) |
| 35 | |
Alexander Graf | 73a1810 | 2010-07-29 14:47:58 +0200 | [diff] [blame^] | 36 | #define KVM_MASK_RT 0x03e00000 |
| 37 | |
| 38 | static bool kvm_patching_worked = true; |
| 39 | |
| 40 | static inline void kvm_patch_ins(u32 *inst, u32 new_inst) |
| 41 | { |
| 42 | *inst = new_inst; |
| 43 | flush_icache_range((ulong)inst, (ulong)inst + 4); |
| 44 | } |
| 45 | |
| 46 | static void kvm_map_magic_page(void *data) |
| 47 | { |
| 48 | kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE, |
| 49 | KVM_MAGIC_PAGE, /* Physical Address */ |
| 50 | KVM_MAGIC_PAGE); /* Effective Address */ |
| 51 | } |
| 52 | |
| 53 | static void kvm_check_ins(u32 *inst) |
| 54 | { |
| 55 | u32 _inst = *inst; |
| 56 | u32 inst_no_rt = _inst & ~KVM_MASK_RT; |
| 57 | u32 inst_rt = _inst & KVM_MASK_RT; |
| 58 | |
| 59 | switch (inst_no_rt) { |
| 60 | } |
| 61 | |
| 62 | switch (_inst) { |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void kvm_use_magic_page(void) |
| 67 | { |
| 68 | u32 *p; |
| 69 | u32 *start, *end; |
| 70 | u32 tmp; |
| 71 | |
| 72 | /* Tell the host to map the magic page to -4096 on all CPUs */ |
| 73 | on_each_cpu(kvm_map_magic_page, NULL, 1); |
| 74 | |
| 75 | /* Quick self-test to see if the mapping works */ |
| 76 | if (__get_user(tmp, (u32*)KVM_MAGIC_PAGE)) { |
| 77 | kvm_patching_worked = false; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | /* Now loop through all code and find instructions */ |
| 82 | start = (void*)_stext; |
| 83 | end = (void*)_etext; |
| 84 | |
| 85 | for (p = start; p < end; p++) |
| 86 | kvm_check_ins(p); |
| 87 | |
| 88 | printk(KERN_INFO "KVM: Live patching for a fast VM %s\n", |
| 89 | kvm_patching_worked ? "worked" : "failed"); |
| 90 | } |
| 91 | |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 92 | unsigned long kvm_hypercall(unsigned long *in, |
| 93 | unsigned long *out, |
| 94 | unsigned long nr) |
| 95 | { |
| 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"); |
| 107 | |
| 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), |
| 111 | "=r"(r12) |
| 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"); |
| 115 | |
| 116 | out[0] = r4; |
| 117 | out[1] = r5; |
| 118 | out[2] = r6; |
| 119 | out[3] = r7; |
| 120 | out[4] = r8; |
| 121 | out[5] = r9; |
| 122 | out[6] = r10; |
| 123 | out[7] = r11; |
| 124 | |
| 125 | return r3; |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(kvm_hypercall); |
Alexander Graf | 73a1810 | 2010-07-29 14:47:58 +0200 | [diff] [blame^] | 128 | |
| 129 | static int kvm_para_setup(void) |
| 130 | { |
| 131 | extern u32 kvm_hypercall_start; |
| 132 | struct device_node *hyper_node; |
| 133 | u32 *insts; |
| 134 | int len, i; |
| 135 | |
| 136 | hyper_node = of_find_node_by_path("/hypervisor"); |
| 137 | if (!hyper_node) |
| 138 | return -1; |
| 139 | |
| 140 | insts = (u32*)of_get_property(hyper_node, "hcall-instructions", &len); |
| 141 | if (len % 4) |
| 142 | return -1; |
| 143 | if (len > (4 * 4)) |
| 144 | return -1; |
| 145 | |
| 146 | for (i = 0; i < (len / 4); i++) |
| 147 | kvm_patch_ins(&(&kvm_hypercall_start)[i], insts[i]); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int __init kvm_guest_init(void) |
| 153 | { |
| 154 | if (!kvm_para_available()) |
| 155 | return 0; |
| 156 | |
| 157 | if (kvm_para_setup()) |
| 158 | return 0; |
| 159 | |
| 160 | if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE)) |
| 161 | kvm_use_magic_page(); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | postcore_initcall(kvm_guest_init); |