]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/xen/privcmd.c
Merge tag 'v3.7' into stable/for-linus-3.8
[karo-tx-linux.git] / drivers / xen / privcmd.c
1 /******************************************************************************
2  * privcmd.c
3  *
4  * Interface to privileged domain-0 commands.
5  *
6  * Copyright (c) 2002-2004, K A Fraser, B Dragovic
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/mm.h>
16 #include <linux/mman.h>
17 #include <linux/uaccess.h>
18 #include <linux/swap.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/seq_file.h>
22 #include <linux/miscdevice.h>
23
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/tlb.h>
27 #include <asm/xen/hypervisor.h>
28 #include <asm/xen/hypercall.h>
29
30 #include <xen/xen.h>
31 #include <xen/privcmd.h>
32 #include <xen/interface/xen.h>
33 #include <xen/features.h>
34 #include <xen/page.h>
35 #include <xen/xen-ops.h>
36 #include <xen/balloon.h>
37
38 #include "privcmd.h"
39
40 MODULE_LICENSE("GPL");
41
42 #define PRIV_VMA_LOCKED ((void *)1)
43
44 #ifndef HAVE_ARCH_PRIVCMD_MMAP
45 static int privcmd_enforce_singleshot_mapping(struct vm_area_struct *vma);
46 #endif
47
48 static long privcmd_ioctl_hypercall(void __user *udata)
49 {
50         struct privcmd_hypercall hypercall;
51         long ret;
52
53         if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
54                 return -EFAULT;
55
56         ret = privcmd_call(hypercall.op,
57                            hypercall.arg[0], hypercall.arg[1],
58                            hypercall.arg[2], hypercall.arg[3],
59                            hypercall.arg[4]);
60
61         return ret;
62 }
63
64 static void free_page_list(struct list_head *pages)
65 {
66         struct page *p, *n;
67
68         list_for_each_entry_safe(p, n, pages, lru)
69                 __free_page(p);
70
71         INIT_LIST_HEAD(pages);
72 }
73
74 /*
75  * Given an array of items in userspace, return a list of pages
76  * containing the data.  If copying fails, either because of memory
77  * allocation failure or a problem reading user memory, return an
78  * error code; its up to the caller to dispose of any partial list.
79  */
80 static int gather_array(struct list_head *pagelist,
81                         unsigned nelem, size_t size,
82                         const void __user *data)
83 {
84         unsigned pageidx;
85         void *pagedata;
86         int ret;
87
88         if (size > PAGE_SIZE)
89                 return 0;
90
91         pageidx = PAGE_SIZE;
92         pagedata = NULL;        /* quiet, gcc */
93         while (nelem--) {
94                 if (pageidx > PAGE_SIZE-size) {
95                         struct page *page = alloc_page(GFP_KERNEL);
96
97                         ret = -ENOMEM;
98                         if (page == NULL)
99                                 goto fail;
100
101                         pagedata = page_address(page);
102
103                         list_add_tail(&page->lru, pagelist);
104                         pageidx = 0;
105                 }
106
107                 ret = -EFAULT;
108                 if (copy_from_user(pagedata + pageidx, data, size))
109                         goto fail;
110
111                 data += size;
112                 pageidx += size;
113         }
114
115         ret = 0;
116
117 fail:
118         return ret;
119 }
120
121 /*
122  * Call function "fn" on each element of the array fragmented
123  * over a list of pages.
124  */
125 static int traverse_pages(unsigned nelem, size_t size,
126                           struct list_head *pos,
127                           int (*fn)(void *data, void *state),
128                           void *state)
129 {
130         void *pagedata;
131         unsigned pageidx;
132         int ret = 0;
133
134         BUG_ON(size > PAGE_SIZE);
135
136         pageidx = PAGE_SIZE;
137         pagedata = NULL;        /* hush, gcc */
138
139         while (nelem--) {
140                 if (pageidx > PAGE_SIZE-size) {
141                         struct page *page;
142                         pos = pos->next;
143                         page = list_entry(pos, struct page, lru);
144                         pagedata = page_address(page);
145                         pageidx = 0;
146                 }
147
148                 ret = (*fn)(pagedata + pageidx, state);
149                 if (ret)
150                         break;
151                 pageidx += size;
152         }
153
154         return ret;
155 }
156
157 struct mmap_mfn_state {
158         unsigned long va;
159         struct vm_area_struct *vma;
160         domid_t domain;
161 };
162
163 static int mmap_mfn_range(void *data, void *state)
164 {
165         struct privcmd_mmap_entry *msg = data;
166         struct mmap_mfn_state *st = state;
167         struct vm_area_struct *vma = st->vma;
168         int rc;
169
170         /* Do not allow range to wrap the address space. */
171         if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
172             ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
173                 return -EINVAL;
174
175         /* Range chunks must be contiguous in va space. */
176         if ((msg->va != st->va) ||
177             ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
178                 return -EINVAL;
179
180         rc = xen_remap_domain_mfn_range(vma,
181                                         msg->va & PAGE_MASK,
182                                         msg->mfn, msg->npages,
183                                         vma->vm_page_prot,
184                                         st->domain, NULL);
185         if (rc < 0)
186                 return rc;
187
188         st->va += msg->npages << PAGE_SHIFT;
189
190         return 0;
191 }
192
193 static long privcmd_ioctl_mmap(void __user *udata)
194 {
195         struct privcmd_mmap mmapcmd;
196         struct mm_struct *mm = current->mm;
197         struct vm_area_struct *vma;
198         int rc;
199         LIST_HEAD(pagelist);
200         struct mmap_mfn_state state;
201
202         /* We only support privcmd_ioctl_mmap_batch for auto translated. */
203         if (xen_feature(XENFEAT_auto_translated_physmap))
204                 return -ENOSYS;
205
206         if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
207                 return -EFAULT;
208
209         rc = gather_array(&pagelist,
210                           mmapcmd.num, sizeof(struct privcmd_mmap_entry),
211                           mmapcmd.entry);
212
213         if (rc || list_empty(&pagelist))
214                 goto out;
215
216         down_write(&mm->mmap_sem);
217
218         {
219                 struct page *page = list_first_entry(&pagelist,
220                                                      struct page, lru);
221                 struct privcmd_mmap_entry *msg = page_address(page);
222
223                 vma = find_vma(mm, msg->va);
224                 rc = -EINVAL;
225
226                 if (!vma || (msg->va != vma->vm_start) ||
227                     !privcmd_enforce_singleshot_mapping(vma))
228                         goto out_up;
229         }
230
231         state.va = vma->vm_start;
232         state.vma = vma;
233         state.domain = mmapcmd.dom;
234
235         rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
236                             &pagelist,
237                             mmap_mfn_range, &state);
238
239
240 out_up:
241         up_write(&mm->mmap_sem);
242
243 out:
244         free_page_list(&pagelist);
245
246         return rc;
247 }
248
249 struct mmap_batch_state {
250         domid_t domain;
251         unsigned long va;
252         struct vm_area_struct *vma;
253         int index;
254         /* A tristate:
255          *      0 for no errors
256          *      1 if at least one error has happened (and no
257          *          -ENOENT errors have happened)
258          *      -ENOENT if at least 1 -ENOENT has happened.
259          */
260         int global_error;
261         /* An array for individual errors */
262         int *err;
263
264         /* User-space mfn array to store errors in the second pass for V1. */
265         xen_pfn_t __user *user_mfn;
266 };
267
268 /* auto translated dom0 note: if domU being created is PV, then mfn is
269  * mfn(addr on bus). If it's auto xlated, then mfn is pfn (input to HAP).
270  */
271 static int mmap_batch_fn(void *data, void *state)
272 {
273         xen_pfn_t *mfnp = data;
274         struct mmap_batch_state *st = state;
275         struct vm_area_struct *vma = st->vma;
276         struct page **pages = vma->vm_private_data;
277         struct page *cur_page = NULL;
278         int ret;
279
280         if (xen_feature(XENFEAT_auto_translated_physmap))
281                 cur_page = pages[st->index++];
282
283         ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
284                                          st->vma->vm_page_prot, st->domain,
285                                          &cur_page);
286
287         /* Store error code for second pass. */
288         *(st->err++) = ret;
289
290         /* And see if it affects the global_error. */
291         if (ret < 0) {
292                 if (ret == -ENOENT)
293                         st->global_error = -ENOENT;
294                 else {
295                         /* Record that at least one error has happened. */
296                         if (st->global_error == 0)
297                                 st->global_error = 1;
298                 }
299         }
300         st->va += PAGE_SIZE;
301
302         return 0;
303 }
304
305 static int mmap_return_errors_v1(void *data, void *state)
306 {
307         xen_pfn_t *mfnp = data;
308         struct mmap_batch_state *st = state;
309         int err = *(st->err++);
310
311         /*
312          * V1 encodes the error codes in the 32bit top nibble of the
313          * mfn (with its known limitations vis-a-vis 64 bit callers).
314          */
315         *mfnp |= (err == -ENOENT) ?
316                                 PRIVCMD_MMAPBATCH_PAGED_ERROR :
317                                 PRIVCMD_MMAPBATCH_MFN_ERROR;
318         return __put_user(*mfnp, st->user_mfn++);
319 }
320
321 /* Allocate pfns that are then mapped with gmfns from foreign domid. Update
322  * the vma with the page info to use later.
323  * Returns: 0 if success, otherwise -errno
324  */
325 static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
326 {
327         int rc;
328         struct page **pages;
329
330         pages = kcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
331         if (pages == NULL)
332                 return -ENOMEM;
333
334         rc = alloc_xenballooned_pages(numpgs, pages, 0);
335         if (rc != 0) {
336                 pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
337                         numpgs, rc);
338                 kfree(pages);
339                 return -ENOMEM;
340         }
341         BUG_ON(vma->vm_private_data != PRIV_VMA_LOCKED);
342         vma->vm_private_data = pages;
343
344         return 0;
345 }
346
347 static struct vm_operations_struct privcmd_vm_ops;
348
349 static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
350 {
351         int ret;
352         struct privcmd_mmapbatch_v2 m;
353         struct mm_struct *mm = current->mm;
354         struct vm_area_struct *vma;
355         unsigned long nr_pages;
356         LIST_HEAD(pagelist);
357         int *err_array = NULL;
358         struct mmap_batch_state state;
359
360         switch (version) {
361         case 1:
362                 if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
363                         return -EFAULT;
364                 /* Returns per-frame error in m.arr. */
365                 m.err = NULL;
366                 if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
367                         return -EFAULT;
368                 break;
369         case 2:
370                 if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
371                         return -EFAULT;
372                 /* Returns per-frame error code in m.err. */
373                 if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
374                         return -EFAULT;
375                 break;
376         default:
377                 return -EINVAL;
378         }
379
380         nr_pages = m.num;
381         if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
382                 return -EINVAL;
383
384         ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
385
386         if (ret)
387                 goto out;
388         if (list_empty(&pagelist)) {
389                 ret = -EINVAL;
390                 goto out;
391         }
392
393         err_array = kcalloc(m.num, sizeof(int), GFP_KERNEL);
394         if (err_array == NULL) {
395                 ret = -ENOMEM;
396                 goto out;
397         }
398
399         down_write(&mm->mmap_sem);
400
401         vma = find_vma(mm, m.addr);
402         if (!vma ||
403             vma->vm_ops != &privcmd_vm_ops ||
404             (m.addr != vma->vm_start) ||
405             ((m.addr + (nr_pages << PAGE_SHIFT)) != vma->vm_end) ||
406             !privcmd_enforce_singleshot_mapping(vma)) {
407                 up_write(&mm->mmap_sem);
408                 ret = -EINVAL;
409                 goto out;
410         }
411         if (xen_feature(XENFEAT_auto_translated_physmap)) {
412                 ret = alloc_empty_pages(vma, m.num);
413                 if (ret < 0) {
414                         up_write(&mm->mmap_sem);
415                         goto out;
416                 }
417         }
418
419         state.domain        = m.dom;
420         state.vma           = vma;
421         state.va            = m.addr;
422         state.index         = 0;
423         state.global_error  = 0;
424         state.err           = err_array;
425
426         /* mmap_batch_fn guarantees ret == 0 */
427         BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
428                              &pagelist, mmap_batch_fn, &state));
429
430         up_write(&mm->mmap_sem);
431
432         if (version == 1) {
433                 if (state.global_error) {
434                         /* Write back errors in second pass. */
435                         state.user_mfn = (xen_pfn_t *)m.arr;
436                         state.err      = err_array;
437                         ret = traverse_pages(m.num, sizeof(xen_pfn_t),
438                                              &pagelist, mmap_return_errors_v1, &state);
439                 } else
440                         ret = 0;
441
442         } else if (version == 2) {
443                 ret = __copy_to_user(m.err, err_array, m.num * sizeof(int));
444                 if (ret)
445                         ret = -EFAULT;
446         }
447
448         /* If we have not had any EFAULT-like global errors then set the global
449          * error to -ENOENT if necessary. */
450         if ((ret == 0) && (state.global_error == -ENOENT))
451                 ret = -ENOENT;
452
453 out:
454         kfree(err_array);
455         free_page_list(&pagelist);
456
457         return ret;
458 }
459
460 static long privcmd_ioctl(struct file *file,
461                           unsigned int cmd, unsigned long data)
462 {
463         int ret = -ENOSYS;
464         void __user *udata = (void __user *) data;
465
466         switch (cmd) {
467         case IOCTL_PRIVCMD_HYPERCALL:
468                 ret = privcmd_ioctl_hypercall(udata);
469                 break;
470
471         case IOCTL_PRIVCMD_MMAP:
472                 ret = privcmd_ioctl_mmap(udata);
473                 break;
474
475         case IOCTL_PRIVCMD_MMAPBATCH:
476                 ret = privcmd_ioctl_mmap_batch(udata, 1);
477                 break;
478
479         case IOCTL_PRIVCMD_MMAPBATCH_V2:
480                 ret = privcmd_ioctl_mmap_batch(udata, 2);
481                 break;
482
483         default:
484                 ret = -EINVAL;
485                 break;
486         }
487
488         return ret;
489 }
490
491 static void privcmd_close(struct vm_area_struct *vma)
492 {
493         struct page **pages = vma->vm_private_data;
494         int numpgs = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
495
496         if (!xen_feature(XENFEAT_auto_translated_physmap || !numpgs || !pages))
497                 return;
498
499         xen_unmap_domain_mfn_range(vma, numpgs, pages);
500         free_xenballooned_pages(numpgs, pages);
501         kfree(pages);
502 }
503
504 static int privcmd_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
505 {
506         printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
507                vma, vma->vm_start, vma->vm_end,
508                vmf->pgoff, vmf->virtual_address);
509
510         return VM_FAULT_SIGBUS;
511 }
512
513 static struct vm_operations_struct privcmd_vm_ops = {
514         .close = privcmd_close,
515         .fault = privcmd_fault
516 };
517
518 static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
519 {
520         /* DONTCOPY is essential for Xen because copy_page_range doesn't know
521          * how to recreate these mappings */
522         vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTCOPY |
523                          VM_DONTEXPAND | VM_DONTDUMP;
524         vma->vm_ops = &privcmd_vm_ops;
525         vma->vm_private_data = NULL;
526
527         return 0;
528 }
529
530 static int privcmd_enforce_singleshot_mapping(struct vm_area_struct *vma)
531 {
532         return !cmpxchg(&vma->vm_private_data, NULL, PRIV_VMA_LOCKED);
533 }
534
535 const struct file_operations xen_privcmd_fops = {
536         .owner = THIS_MODULE,
537         .unlocked_ioctl = privcmd_ioctl,
538         .mmap = privcmd_mmap,
539 };
540 EXPORT_SYMBOL_GPL(xen_privcmd_fops);
541
542 static struct miscdevice privcmd_dev = {
543         .minor = MISC_DYNAMIC_MINOR,
544         .name = "xen/privcmd",
545         .fops = &xen_privcmd_fops,
546 };
547
548 static int __init privcmd_init(void)
549 {
550         int err;
551
552         if (!xen_domain())
553                 return -ENODEV;
554
555         err = misc_register(&privcmd_dev);
556         if (err != 0) {
557                 printk(KERN_ERR "Could not register Xen privcmd device\n");
558                 return err;
559         }
560         return 0;
561 }
562
563 static void __exit privcmd_exit(void)
564 {
565         misc_deregister(&privcmd_dev);
566 }
567
568 module_init(privcmd_init);
569 module_exit(privcmd_exit);