1 /* 2 * Copyright (C) 2011. Freescale Inc. All rights reserved. 3 * 4 * Authors: 5 * Alexander Graf <agraf@suse.de> 6 * Paul Mackerras <paulus@samba.org> 7 * 8 * Description: 9 * 10 * Hypercall handling for running PAPR guests in PR KVM on Book 3S 11 * processors. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License, version 2, as 15 * published by the Free Software Foundation. 16 */ 17 18 #include <linux/anon_inodes.h> 19 20 #include <asm/uaccess.h> 21 #include <asm/kvm_ppc.h> 22 #include <asm/kvm_book3s.h> 23 24 #define HPTE_SIZE 16 /* bytes per HPT entry */ 25 26 static unsigned long get_pteg_addr(struct kvm_vcpu *vcpu, long pte_index) 27 { 28 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); 29 unsigned long pteg_addr; 30 31 pte_index <<= 4; 32 pte_index &= ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1) << 7 | 0x70; 33 pteg_addr = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL; 34 pteg_addr |= pte_index; 35 36 return pteg_addr; 37 } 38 39 static int kvmppc_h_pr_enter(struct kvm_vcpu *vcpu) 40 { 41 long flags = kvmppc_get_gpr(vcpu, 4); 42 long pte_index = kvmppc_get_gpr(vcpu, 5); 43 unsigned long pteg[2 * 8]; 44 unsigned long pteg_addr, i, *hpte; 45 long int ret; 46 47 i = pte_index & 7; 48 pte_index &= ~7UL; 49 pteg_addr = get_pteg_addr(vcpu, pte_index); 50 51 mutex_lock(&vcpu->kvm->arch.hpt_mutex); 52 copy_from_user(pteg, (void __user *)pteg_addr, sizeof(pteg)); 53 hpte = pteg; 54 55 ret = H_PTEG_FULL; 56 if (likely((flags & H_EXACT) == 0)) { 57 for (i = 0; ; ++i) { 58 if (i == 8) 59 goto done; 60 if ((*hpte & HPTE_V_VALID) == 0) 61 break; 62 hpte += 2; 63 } 64 } else { 65 hpte += i * 2; 66 if (*hpte & HPTE_V_VALID) 67 goto done; 68 } 69 70 hpte[0] = kvmppc_get_gpr(vcpu, 6); 71 hpte[1] = kvmppc_get_gpr(vcpu, 7); 72 pteg_addr += i * HPTE_SIZE; 73 copy_to_user((void __user *)pteg_addr, hpte, HPTE_SIZE); 74 kvmppc_set_gpr(vcpu, 4, pte_index | i); 75 ret = H_SUCCESS; 76 77 done: 78 mutex_unlock(&vcpu->kvm->arch.hpt_mutex); 79 kvmppc_set_gpr(vcpu, 3, ret); 80 81 return EMULATE_DONE; 82 } 83 84 static int kvmppc_h_pr_remove(struct kvm_vcpu *vcpu) 85 { 86 unsigned long flags= kvmppc_get_gpr(vcpu, 4); 87 unsigned long pte_index = kvmppc_get_gpr(vcpu, 5); 88 unsigned long avpn = kvmppc_get_gpr(vcpu, 6); 89 unsigned long v = 0, pteg, rb; 90 unsigned long pte[2]; 91 long int ret; 92 93 pteg = get_pteg_addr(vcpu, pte_index); 94 mutex_lock(&vcpu->kvm->arch.hpt_mutex); 95 copy_from_user(pte, (void __user *)pteg, sizeof(pte)); 96 97 ret = H_NOT_FOUND; 98 if ((pte[0] & HPTE_V_VALID) == 0 || 99 ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn) || 100 ((flags & H_ANDCOND) && (pte[0] & avpn) != 0)) 101 goto done; 102 103 copy_to_user((void __user *)pteg, &v, sizeof(v)); 104 105 rb = compute_tlbie_rb(pte[0], pte[1], pte_index); 106 vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false); 107 108 ret = H_SUCCESS; 109 kvmppc_set_gpr(vcpu, 4, pte[0]); 110 kvmppc_set_gpr(vcpu, 5, pte[1]); 111 112 done: 113 mutex_unlock(&vcpu->kvm->arch.hpt_mutex); 114 kvmppc_set_gpr(vcpu, 3, ret); 115 116 return EMULATE_DONE; 117 } 118 119 /* Request defs for kvmppc_h_pr_bulk_remove() */ 120 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL 121 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL 122 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL 123 #define H_BULK_REMOVE_END 0xc000000000000000ULL 124 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL 125 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL 126 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL 127 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL 128 #define H_BULK_REMOVE_HW 0x3000000000000000ULL 129 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL 130 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL 131 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL 132 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL 133 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL 134 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL 135 #define H_BULK_REMOVE_MAX_BATCH 4 136 137 static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu) 138 { 139 int i; 140 int paramnr = 4; 141 int ret = H_SUCCESS; 142 143 mutex_lock(&vcpu->kvm->arch.hpt_mutex); 144 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) { 145 unsigned long tsh = kvmppc_get_gpr(vcpu, paramnr+(2*i)); 146 unsigned long tsl = kvmppc_get_gpr(vcpu, paramnr+(2*i)+1); 147 unsigned long pteg, rb, flags; 148 unsigned long pte[2]; 149 unsigned long v = 0; 150 151 if ((tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) { 152 break; /* Exit success */ 153 } else if ((tsh & H_BULK_REMOVE_TYPE) != 154 H_BULK_REMOVE_REQUEST) { 155 ret = H_PARAMETER; 156 break; /* Exit fail */ 157 } 158 159 tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS; 160 tsh |= H_BULK_REMOVE_RESPONSE; 161 162 if ((tsh & H_BULK_REMOVE_ANDCOND) && 163 (tsh & H_BULK_REMOVE_AVPN)) { 164 tsh |= H_BULK_REMOVE_PARM; 165 kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh); 166 ret = H_PARAMETER; 167 break; /* Exit fail */ 168 } 169 170 pteg = get_pteg_addr(vcpu, tsh & H_BULK_REMOVE_PTEX); 171 copy_from_user(pte, (void __user *)pteg, sizeof(pte)); 172 173 /* tsl = AVPN */ 174 flags = (tsh & H_BULK_REMOVE_FLAGS) >> 26; 175 176 if ((pte[0] & HPTE_V_VALID) == 0 || 177 ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != tsl) || 178 ((flags & H_ANDCOND) && (pte[0] & tsl) != 0)) { 179 tsh |= H_BULK_REMOVE_NOT_FOUND; 180 } else { 181 /* Splat the pteg in (userland) hpt */ 182 copy_to_user((void __user *)pteg, &v, sizeof(v)); 183 184 rb = compute_tlbie_rb(pte[0], pte[1], 185 tsh & H_BULK_REMOVE_PTEX); 186 vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false); 187 tsh |= H_BULK_REMOVE_SUCCESS; 188 tsh |= (pte[1] & (HPTE_R_C | HPTE_R_R)) << 43; 189 } 190 kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh); 191 } 192 mutex_unlock(&vcpu->kvm->arch.hpt_mutex); 193 kvmppc_set_gpr(vcpu, 3, ret); 194 195 return EMULATE_DONE; 196 } 197 198 static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu) 199 { 200 unsigned long flags = kvmppc_get_gpr(vcpu, 4); 201 unsigned long pte_index = kvmppc_get_gpr(vcpu, 5); 202 unsigned long avpn = kvmppc_get_gpr(vcpu, 6); 203 unsigned long rb, pteg, r, v; 204 unsigned long pte[2]; 205 long int ret; 206 207 pteg = get_pteg_addr(vcpu, pte_index); 208 mutex_lock(&vcpu->kvm->arch.hpt_mutex); 209 copy_from_user(pte, (void __user *)pteg, sizeof(pte)); 210 211 ret = H_NOT_FOUND; 212 if ((pte[0] & HPTE_V_VALID) == 0 || 213 ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn)) 214 goto done; 215 216 v = pte[0]; 217 r = pte[1]; 218 r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_HI | 219 HPTE_R_KEY_LO); 220 r |= (flags << 55) & HPTE_R_PP0; 221 r |= (flags << 48) & HPTE_R_KEY_HI; 222 r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO); 223 224 pte[1] = r; 225 226 rb = compute_tlbie_rb(v, r, pte_index); 227 vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false); 228 copy_to_user((void __user *)pteg, pte, sizeof(pte)); 229 ret = H_SUCCESS; 230 231 done: 232 mutex_unlock(&vcpu->kvm->arch.hpt_mutex); 233 kvmppc_set_gpr(vcpu, 3, ret); 234 235 return EMULATE_DONE; 236 } 237 238 static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu) 239 { 240 unsigned long liobn = kvmppc_get_gpr(vcpu, 4); 241 unsigned long ioba = kvmppc_get_gpr(vcpu, 5); 242 unsigned long tce = kvmppc_get_gpr(vcpu, 6); 243 long rc; 244 245 rc = kvmppc_h_put_tce(vcpu, liobn, ioba, tce); 246 if (rc == H_TOO_HARD) 247 return EMULATE_FAIL; 248 kvmppc_set_gpr(vcpu, 3, rc); 249 return EMULATE_DONE; 250 } 251 252 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd) 253 { 254 long rc = kvmppc_xics_hcall(vcpu, cmd); 255 kvmppc_set_gpr(vcpu, 3, rc); 256 return EMULATE_DONE; 257 } 258 259 int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd) 260 { 261 switch (cmd) { 262 case H_ENTER: 263 return kvmppc_h_pr_enter(vcpu); 264 case H_REMOVE: 265 return kvmppc_h_pr_remove(vcpu); 266 case H_PROTECT: 267 return kvmppc_h_pr_protect(vcpu); 268 case H_BULK_REMOVE: 269 return kvmppc_h_pr_bulk_remove(vcpu); 270 case H_PUT_TCE: 271 return kvmppc_h_pr_put_tce(vcpu); 272 case H_CEDE: 273 vcpu->arch.shared->msr |= MSR_EE; 274 kvm_vcpu_block(vcpu); 275 clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 276 vcpu->stat.halt_wakeup++; 277 return EMULATE_DONE; 278 case H_XIRR: 279 case H_CPPR: 280 case H_EOI: 281 case H_IPI: 282 case H_IPOLL: 283 case H_XIRR_X: 284 if (kvmppc_xics_enabled(vcpu)) 285 return kvmppc_h_pr_xics_hcall(vcpu, cmd); 286 break; 287 case H_RTAS: 288 if (list_empty(&vcpu->kvm->arch.rtas_tokens)) 289 return RESUME_HOST; 290 if (kvmppc_rtas_hcall(vcpu)) 291 break; 292 kvmppc_set_gpr(vcpu, 3, 0); 293 return EMULATE_DONE; 294 } 295 296 return EMULATE_FAIL; 297 } 298