ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c @ 6711:4cdf880c9463

Bug fix on x86_64 increase_reservation
Signed-off-by: ian@xensource.com
author iap10@firebug.cl.cam.ac.uk
date Thu Sep 08 20:39:58 2005 +0000 (2005-09-08)
parents a9a78ca76cd2
children 2704a88c3295 cdfa7dd00c44
line source
1 /******************************************************************************
2 * privcmd.c
3 *
4 * Interface to privileged domain-0 commands.
5 *
6 * Copyright (c) 2002-2004, K A Fraser, B Dragovic
7 */
9 #include <linux/config.h>
10 #include <linux/kernel.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/swap.h>
18 #include <linux/smp_lock.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/seq_file.h>
22 #include <linux/kthread.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/uaccess.h>
27 #include <asm/tlb.h>
28 #include <asm-xen/linux-public/privcmd.h>
29 #include <asm-xen/xen-public/dom0_ops.h>
30 #include <asm-xen/xen_proc.h>
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
33 #define pud_t pgd_t
34 #define pud_offset(d, va) d
35 #endif
37 static struct proc_dir_entry *privcmd_intf;
39 static int privcmd_ioctl(struct inode *inode, struct file *file,
40 unsigned int cmd, unsigned long data)
41 {
42 int ret = -ENOSYS;
44 switch ( cmd )
45 {
46 case IOCTL_PRIVCMD_HYPERCALL:
47 {
48 privcmd_hypercall_t hypercall;
50 if ( copy_from_user(&hypercall, (void *)data, sizeof(hypercall)) )
51 return -EFAULT;
53 #if defined(__i386__)
54 __asm__ __volatile__ (
55 "pushl %%ebx; pushl %%ecx; pushl %%edx; pushl %%esi; pushl %%edi; "
56 "movl 4(%%eax),%%ebx ;"
57 "movl 8(%%eax),%%ecx ;"
58 "movl 12(%%eax),%%edx ;"
59 "movl 16(%%eax),%%esi ;"
60 "movl 20(%%eax),%%edi ;"
61 "movl (%%eax),%%eax ;"
62 TRAP_INSTR "; "
63 "popl %%edi; popl %%esi; popl %%edx; popl %%ecx; popl %%ebx"
64 : "=a" (ret) : "0" (&hypercall) : "memory" );
65 #elif defined (__x86_64__)
66 {
67 long ign1, ign2, ign3;
68 __asm__ __volatile__ (
69 "movq %8,%%r10; movq %9,%%r8;" TRAP_INSTR
70 : "=a" (ret), "=D" (ign1), "=S" (ign2), "=d" (ign3)
71 : "0" ((unsigned long)hypercall.op),
72 "1" ((unsigned long)hypercall.arg[0]),
73 "2" ((unsigned long)hypercall.arg[1]),
74 "3" ((unsigned long)hypercall.arg[2]),
75 "g" ((unsigned long)hypercall.arg[3]),
76 "g" ((unsigned long)hypercall.arg[4])
77 : "r11","rcx","r8","r10","memory");
78 }
79 #endif
80 }
81 break;
83 #if defined(CONFIG_XEN_PRIVILEGED_GUEST)
84 case IOCTL_PRIVCMD_MMAP:
85 {
86 #define PRIVCMD_MMAP_SZ 32
87 privcmd_mmap_t mmapcmd;
88 privcmd_mmap_entry_t msg[PRIVCMD_MMAP_SZ], *p;
89 int i, rc;
91 if ( copy_from_user(&mmapcmd, (void *)data, sizeof(mmapcmd)) )
92 return -EFAULT;
94 p = mmapcmd.entry;
96 for (i=0; i<mmapcmd.num; i+=PRIVCMD_MMAP_SZ, p+=PRIVCMD_MMAP_SZ)
97 {
98 int j, n = ((mmapcmd.num-i)>PRIVCMD_MMAP_SZ)?
99 PRIVCMD_MMAP_SZ:(mmapcmd.num-i);
102 if ( copy_from_user(&msg, p, n*sizeof(privcmd_mmap_entry_t)) )
103 return -EFAULT;
105 for ( j = 0; j < n; j++ )
106 {
107 struct vm_area_struct *vma =
108 find_vma( current->mm, msg[j].va );
110 if ( !vma )
111 return -EINVAL;
113 if ( msg[j].va > PAGE_OFFSET )
114 return -EINVAL;
116 if ( (msg[j].va + (msg[j].npages<<PAGE_SHIFT)) > vma->vm_end )
117 return -EINVAL;
119 if ( (rc = direct_remap_pfn_range(vma->vm_mm,
120 msg[j].va&PAGE_MASK,
121 msg[j].mfn,
122 msg[j].npages<<PAGE_SHIFT,
123 vma->vm_page_prot,
124 mmapcmd.dom)) < 0 )
125 return rc;
126 }
127 }
128 ret = 0;
129 }
130 break;
132 case IOCTL_PRIVCMD_MMAPBATCH:
133 {
134 mmu_update_t u;
135 privcmd_mmapbatch_t m;
136 struct vm_area_struct *vma = NULL;
137 unsigned long *p, addr;
138 unsigned long mfn, ptep;
139 int i;
141 if ( copy_from_user(&m, (void *)data, sizeof(m)) )
142 { ret = -EFAULT; goto batch_err; }
144 vma = find_vma( current->mm, m.addr );
146 if ( !vma )
147 { ret = -EINVAL; goto batch_err; }
149 if ( m.addr > PAGE_OFFSET )
150 { ret = -EFAULT; goto batch_err; }
152 if ( (m.addr + (m.num<<PAGE_SHIFT)) > vma->vm_end )
153 { ret = -EFAULT; goto batch_err; }
155 p = m.arr;
156 addr = m.addr;
157 for ( i = 0; i < m.num; i++, addr += PAGE_SIZE, p++ )
158 {
159 if ( get_user(mfn, p) )
160 return -EFAULT;
162 ret = create_lookup_pte_addr(vma->vm_mm, addr, &ptep);
163 if (ret)
164 goto batch_err;
166 u.val = pte_val_ma(pfn_pte_ma(mfn, vma->vm_page_prot));
167 u.ptr = ptep;
169 if ( unlikely(HYPERVISOR_mmu_update(&u, 1, NULL, m.dom) < 0) )
170 put_user(0xF0000000 | mfn, p);
171 }
173 ret = 0;
174 break;
176 batch_err:
177 printk("batch_err ret=%d vma=%p addr=%lx num=%d arr=%p %lx-%lx\n",
178 ret, vma, m.addr, m.num, m.arr,
179 vma ? vma->vm_start : 0, vma ? vma->vm_end : 0);
180 break;
181 }
182 break;
183 #endif
185 case IOCTL_PRIVCMD_GET_MACH2PHYS_START_MFN:
186 {
187 unsigned long m2pv = (unsigned long)machine_to_phys_mapping;
188 pgd_t *pgd = pgd_offset_k(m2pv);
189 pud_t *pud = pud_offset(pgd, m2pv);
190 pmd_t *pmd = pmd_offset(pud, m2pv);
191 unsigned long m2p_start_mfn = (*(unsigned long *)pmd) >> PAGE_SHIFT;
192 ret = put_user(m2p_start_mfn, (unsigned long *)data) ? -EFAULT: 0;
193 }
194 break;
196 case IOCTL_PRIVCMD_INITDOMAIN_STORE:
197 {
198 extern int do_xenbus_probe(void*);
199 unsigned long page;
201 if (xen_start_info->store_evtchn != 0) {
202 ret = xen_start_info->store_mfn;
203 break;
204 }
206 /* Allocate page. */
207 page = get_zeroed_page(GFP_KERNEL);
208 if (!page) {
209 ret = -ENOMEM;
210 break;
211 }
213 /* We don't refcnt properly, so set reserved on page.
214 * (this allocation is permanent) */
215 SetPageReserved(virt_to_page(page));
217 /* Initial connect. Setup channel and page. */
218 xen_start_info->store_evtchn = data;
219 xen_start_info->store_mfn = pfn_to_mfn(virt_to_phys((void *)page) >>
220 PAGE_SHIFT);
221 ret = xen_start_info->store_mfn;
223 /* We'll return then this will wait for daemon to answer */
224 kthread_run(do_xenbus_probe, NULL, "xenbus_probe");
225 }
226 break;
228 default:
229 ret = -EINVAL;
230 break;
231 }
232 return ret;
233 }
235 static int privcmd_mmap(struct file * file, struct vm_area_struct * vma)
236 {
237 /* DONTCOPY is essential for Xen as copy_page_range is broken. */
238 vma->vm_flags |= VM_RESERVED | VM_IO | VM_DONTCOPY;
240 return 0;
241 }
243 static struct file_operations privcmd_file_ops = {
244 ioctl : privcmd_ioctl,
245 mmap: privcmd_mmap
246 };
249 static int __init privcmd_init(void)
250 {
251 privcmd_intf = create_xen_proc_entry("privcmd", 0400);
252 if ( privcmd_intf != NULL )
253 privcmd_intf->proc_fops = &privcmd_file_ops;
255 return 0;
256 }
258 __initcall(privcmd_init);