~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/virt/kvm/arm/vgic/vgic-kvm-device.c

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * VGIC: KVM DEVICE API
  3  *
  4  * Copyright (C) 2015 ARM Ltd.
  5  * Author: Marc Zyngier <marc.zyngier@arm.com>
  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 #include <linux/kvm_host.h>
 17 #include <kvm/arm_vgic.h>
 18 #include <linux/uaccess.h>
 19 #include <asm/kvm_mmu.h>
 20 #include "vgic.h"
 21 
 22 /* common helpers */
 23 
 24 static int vgic_check_ioaddr(struct kvm *kvm, phys_addr_t *ioaddr,
 25                              phys_addr_t addr, phys_addr_t alignment)
 26 {
 27         if (addr & ~KVM_PHYS_MASK)
 28                 return -E2BIG;
 29 
 30         if (!IS_ALIGNED(addr, alignment))
 31                 return -EINVAL;
 32 
 33         if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
 34                 return -EEXIST;
 35 
 36         return 0;
 37 }
 38 
 39 /**
 40  * kvm_vgic_addr - set or get vgic VM base addresses
 41  * @kvm:   pointer to the vm struct
 42  * @type:  the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
 43  * @addr:  pointer to address value
 44  * @write: if true set the address in the VM address space, if false read the
 45  *          address
 46  *
 47  * Set or get the vgic base addresses for the distributor and the virtual CPU
 48  * interface in the VM physical address space.  These addresses are properties
 49  * of the emulated core/SoC and therefore user space initially knows this
 50  * information.
 51  * Check them for sanity (alignment, double assignment). We can't check for
 52  * overlapping regions in case of a virtual GICv3 here, since we don't know
 53  * the number of VCPUs yet, so we defer this check to map_resources().
 54  */
 55 int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
 56 {
 57         int r = 0;
 58         struct vgic_dist *vgic = &kvm->arch.vgic;
 59         int type_needed;
 60         phys_addr_t *addr_ptr, alignment;
 61 
 62         mutex_lock(&kvm->lock);
 63         switch (type) {
 64         case KVM_VGIC_V2_ADDR_TYPE_DIST:
 65                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
 66                 addr_ptr = &vgic->vgic_dist_base;
 67                 alignment = SZ_4K;
 68                 break;
 69         case KVM_VGIC_V2_ADDR_TYPE_CPU:
 70                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
 71                 addr_ptr = &vgic->vgic_cpu_base;
 72                 alignment = SZ_4K;
 73                 break;
 74 #ifdef CONFIG_KVM_ARM_VGIC_V3
 75         case KVM_VGIC_V3_ADDR_TYPE_DIST:
 76                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
 77                 addr_ptr = &vgic->vgic_dist_base;
 78                 alignment = SZ_64K;
 79                 break;
 80         case KVM_VGIC_V3_ADDR_TYPE_REDIST:
 81                 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
 82                 addr_ptr = &vgic->vgic_redist_base;
 83                 alignment = SZ_64K;
 84                 break;
 85 #endif
 86         default:
 87                 r = -ENODEV;
 88                 goto out;
 89         }
 90 
 91         if (vgic->vgic_model != type_needed) {
 92                 r = -ENODEV;
 93                 goto out;
 94         }
 95 
 96         if (write) {
 97                 r = vgic_check_ioaddr(kvm, addr_ptr, *addr, alignment);
 98                 if (!r)
 99                         *addr_ptr = *addr;
100         } else {
101                 *addr = *addr_ptr;
102         }
103 
104 out:
105         mutex_unlock(&kvm->lock);
106         return r;
107 }
108 
109 static int vgic_set_common_attr(struct kvm_device *dev,
110                                 struct kvm_device_attr *attr)
111 {
112         int r;
113 
114         switch (attr->group) {
115         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
116                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
117                 u64 addr;
118                 unsigned long type = (unsigned long)attr->attr;
119 
120                 if (copy_from_user(&addr, uaddr, sizeof(addr)))
121                         return -EFAULT;
122 
123                 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
124                 return (r == -ENODEV) ? -ENXIO : r;
125         }
126         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
127                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
128                 u32 val;
129                 int ret = 0;
130 
131                 if (get_user(val, uaddr))
132                         return -EFAULT;
133 
134                 /*
135                  * We require:
136                  * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
137                  * - at most 1024 interrupts
138                  * - a multiple of 32 interrupts
139                  */
140                 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
141                     val > VGIC_MAX_RESERVED ||
142                     (val & 31))
143                         return -EINVAL;
144 
145                 mutex_lock(&dev->kvm->lock);
146 
147                 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_spis)
148                         ret = -EBUSY;
149                 else
150                         dev->kvm->arch.vgic.nr_spis =
151                                 val - VGIC_NR_PRIVATE_IRQS;
152 
153                 mutex_unlock(&dev->kvm->lock);
154 
155                 return ret;
156         }
157         case KVM_DEV_ARM_VGIC_GRP_CTRL: {
158                 switch (attr->attr) {
159                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
160                         mutex_lock(&dev->kvm->lock);
161                         r = vgic_init(dev->kvm);
162                         mutex_unlock(&dev->kvm->lock);
163                         return r;
164                 }
165                 break;
166         }
167         }
168 
169         return -ENXIO;
170 }
171 
172 static int vgic_get_common_attr(struct kvm_device *dev,
173                                 struct kvm_device_attr *attr)
174 {
175         int r = -ENXIO;
176 
177         switch (attr->group) {
178         case KVM_DEV_ARM_VGIC_GRP_ADDR: {
179                 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
180                 u64 addr;
181                 unsigned long type = (unsigned long)attr->attr;
182 
183                 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
184                 if (r)
185                         return (r == -ENODEV) ? -ENXIO : r;
186 
187                 if (copy_to_user(uaddr, &addr, sizeof(addr)))
188                         return -EFAULT;
189                 break;
190         }
191         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
192                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
193 
194                 r = put_user(dev->kvm->arch.vgic.nr_spis +
195                              VGIC_NR_PRIVATE_IRQS, uaddr);
196                 break;
197         }
198         }
199 
200         return r;
201 }
202 
203 static int vgic_create(struct kvm_device *dev, u32 type)
204 {
205         return kvm_vgic_create(dev->kvm, type);
206 }
207 
208 static void vgic_destroy(struct kvm_device *dev)
209 {
210         kfree(dev);
211 }
212 
213 void kvm_register_vgic_device(unsigned long type)
214 {
215         switch (type) {
216         case KVM_DEV_TYPE_ARM_VGIC_V2:
217                 kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
218                                         KVM_DEV_TYPE_ARM_VGIC_V2);
219                 break;
220 #ifdef CONFIG_KVM_ARM_VGIC_V3
221         case KVM_DEV_TYPE_ARM_VGIC_V3:
222                 kvm_register_device_ops(&kvm_arm_vgic_v3_ops,
223                                         KVM_DEV_TYPE_ARM_VGIC_V3);
224                 break;
225 #endif
226         }
227 }
228 
229 /** vgic_attr_regs_access: allows user space to read/write VGIC registers
230  *
231  * @dev: kvm device handle
232  * @attr: kvm device attribute
233  * @reg: address the value is read or written
234  * @is_write: write flag
235  *
236  */
237 static int vgic_attr_regs_access(struct kvm_device *dev,
238                                  struct kvm_device_attr *attr,
239                                  u32 *reg, bool is_write)
240 {
241         gpa_t addr;
242         int cpuid, ret, c;
243         struct kvm_vcpu *vcpu, *tmp_vcpu;
244         int vcpu_lock_idx = -1;
245 
246         cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
247                  KVM_DEV_ARM_VGIC_CPUID_SHIFT;
248         vcpu = kvm_get_vcpu(dev->kvm, cpuid);
249         addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
250 
251         mutex_lock(&dev->kvm->lock);
252 
253         ret = vgic_init(dev->kvm);
254         if (ret)
255                 goto out;
256 
257         if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
258                 ret = -EINVAL;
259                 goto out;
260         }
261 
262         /*
263          * Any time a vcpu is run, vcpu_load is called which tries to grab the
264          * vcpu->mutex.  By grabbing the vcpu->mutex of all VCPUs we ensure
265          * that no other VCPUs are run and fiddle with the vgic state while we
266          * access it.
267          */
268         ret = -EBUSY;
269         kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
270                 if (!mutex_trylock(&tmp_vcpu->mutex))
271                         goto out;
272                 vcpu_lock_idx = c;
273         }
274 
275         switch (attr->group) {
276         case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
277                 ret = vgic_v2_cpuif_uaccess(vcpu, is_write, addr, reg);
278                 break;
279         case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
280                 ret = vgic_v2_dist_uaccess(vcpu, is_write, addr, reg);
281                 break;
282         default:
283                 ret = -EINVAL;
284                 break;
285         }
286 
287 out:
288         for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
289                 tmp_vcpu = kvm_get_vcpu(dev->kvm, vcpu_lock_idx);
290                 mutex_unlock(&tmp_vcpu->mutex);
291         }
292 
293         mutex_unlock(&dev->kvm->lock);
294         return ret;
295 }
296 
297 /* V2 ops */
298 
299 static int vgic_v2_set_attr(struct kvm_device *dev,
300                             struct kvm_device_attr *attr)
301 {
302         int ret;
303 
304         ret = vgic_set_common_attr(dev, attr);
305         if (ret != -ENXIO)
306                 return ret;
307 
308         switch (attr->group) {
309         case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
310         case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
311                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
312                 u32 reg;
313 
314                 if (get_user(reg, uaddr))
315                         return -EFAULT;
316 
317                 return vgic_attr_regs_access(dev, attr, &reg, true);
318         }
319         }
320 
321         return -ENXIO;
322 }
323 
324 static int vgic_v2_get_attr(struct kvm_device *dev,
325                             struct kvm_device_attr *attr)
326 {
327         int ret;
328 
329         ret = vgic_get_common_attr(dev, attr);
330         if (ret != -ENXIO)
331                 return ret;
332 
333         switch (attr->group) {
334         case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
335         case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
336                 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
337                 u32 reg = 0;
338 
339                 ret = vgic_attr_regs_access(dev, attr, &reg, false);
340                 if (ret)
341                         return ret;
342                 return put_user(reg, uaddr);
343         }
344         }
345 
346         return -ENXIO;
347 }
348 
349 static int vgic_v2_has_attr(struct kvm_device *dev,
350                             struct kvm_device_attr *attr)
351 {
352         switch (attr->group) {
353         case KVM_DEV_ARM_VGIC_GRP_ADDR:
354                 switch (attr->attr) {
355                 case KVM_VGIC_V2_ADDR_TYPE_DIST:
356                 case KVM_VGIC_V2_ADDR_TYPE_CPU:
357                         return 0;
358                 }
359                 break;
360         case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
361         case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
362                 return vgic_v2_has_attr_regs(dev, attr);
363         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
364                 return 0;
365         case KVM_DEV_ARM_VGIC_GRP_CTRL:
366                 switch (attr->attr) {
367                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
368                         return 0;
369                 }
370         }
371         return -ENXIO;
372 }
373 
374 struct kvm_device_ops kvm_arm_vgic_v2_ops = {
375         .name = "kvm-arm-vgic-v2",
376         .create = vgic_create,
377         .destroy = vgic_destroy,
378         .set_attr = vgic_v2_set_attr,
379         .get_attr = vgic_v2_get_attr,
380         .has_attr = vgic_v2_has_attr,
381 };
382 
383 /* V3 ops */
384 
385 #ifdef CONFIG_KVM_ARM_VGIC_V3
386 
387 static int vgic_v3_set_attr(struct kvm_device *dev,
388                             struct kvm_device_attr *attr)
389 {
390         return vgic_set_common_attr(dev, attr);
391 }
392 
393 static int vgic_v3_get_attr(struct kvm_device *dev,
394                             struct kvm_device_attr *attr)
395 {
396         return vgic_get_common_attr(dev, attr);
397 }
398 
399 static int vgic_v3_has_attr(struct kvm_device *dev,
400                             struct kvm_device_attr *attr)
401 {
402         switch (attr->group) {
403         case KVM_DEV_ARM_VGIC_GRP_ADDR:
404                 switch (attr->attr) {
405                 case KVM_VGIC_V3_ADDR_TYPE_DIST:
406                 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
407                         return 0;
408                 }
409                 break;
410         case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
411                 return 0;
412         case KVM_DEV_ARM_VGIC_GRP_CTRL:
413                 switch (attr->attr) {
414                 case KVM_DEV_ARM_VGIC_CTRL_INIT:
415                         return 0;
416                 }
417         }
418         return -ENXIO;
419 }
420 
421 struct kvm_device_ops kvm_arm_vgic_v3_ops = {
422         .name = "kvm-arm-vgic-v3",
423         .create = vgic_create,
424         .destroy = vgic_destroy,
425         .set_attr = vgic_v3_set_attr,
426         .get_attr = vgic_v3_get_attr,
427         .has_attr = vgic_v3_has_attr,
428 };
429 
430 #endif /* CONFIG_KVM_ARM_VGIC_V3 */
431 
432 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp