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