ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
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 __asm__ __volatile__ (
67 "movq %5,%%r10; movq %6,%%r8;" TRAP_INSTR
68 : "=a" (ret)
69 : "a" ((unsigned long)hypercall.op),
70 "D" ((unsigned long)hypercall.arg[0]),
71 "S" ((unsigned long)hypercall.arg[1]),
72 "d" ((unsigned long)hypercall.arg[2]),
73 "g" ((unsigned long)hypercall.arg[3]),
74 "g" ((unsigned long)hypercall.arg[4])
75 : "r11","rcx","r8","r10","memory");
76 #endif
77 }
78 break;
80 case IOCTL_PRIVCMD_INITDOMAIN_EVTCHN:
81 {
82 extern int initdom_ctrlif_domcontroller_port;
83 ret = initdom_ctrlif_domcontroller_port;
84 }
85 break;
87 #if defined(CONFIG_XEN_PRIVILEGED_GUEST)
88 case IOCTL_PRIVCMD_MMAP:
89 {
90 #define PRIVCMD_MMAP_SZ 32
91 privcmd_mmap_t mmapcmd;
92 privcmd_mmap_entry_t msg[PRIVCMD_MMAP_SZ], *p;
93 int i, rc;
95 if ( copy_from_user(&mmapcmd, (void *)data, sizeof(mmapcmd)) )
96 return -EFAULT;
98 p = mmapcmd.entry;
100 for (i=0; i<mmapcmd.num; i+=PRIVCMD_MMAP_SZ, p+=PRIVCMD_MMAP_SZ)
101 {
102 int j, n = ((mmapcmd.num-i)>PRIVCMD_MMAP_SZ)?
103 PRIVCMD_MMAP_SZ:(mmapcmd.num-i);
106 if ( copy_from_user(&msg, p, n*sizeof(privcmd_mmap_entry_t)) )
107 return -EFAULT;
109 for ( j = 0; j < n; j++ )
110 {
111 struct vm_area_struct *vma =
112 find_vma( current->mm, msg[j].va );
114 if ( !vma )
115 return -EINVAL;
117 if ( msg[j].va > PAGE_OFFSET )
118 return -EINVAL;
120 if ( (msg[j].va + (msg[j].npages<<PAGE_SHIFT)) > vma->vm_end )
121 return -EINVAL;
123 if ( (rc = direct_remap_area_pages(vma->vm_mm,
124 msg[j].va&PAGE_MASK,
125 msg[j].mfn<<PAGE_SHIFT,
126 msg[j].npages<<PAGE_SHIFT,
127 vma->vm_page_prot,
128 mmapcmd.dom)) < 0 )
129 return rc;
130 }
131 }
132 ret = 0;
133 }
134 break;
136 case IOCTL_PRIVCMD_MMAPBATCH:
137 {
138 mmu_update_t u;
139 privcmd_mmapbatch_t m;
140 struct vm_area_struct *vma = NULL;
141 unsigned long *p, addr;
142 unsigned long mfn, ptep;
143 int i;
145 if ( copy_from_user(&m, (void *)data, sizeof(m)) )
146 { ret = -EFAULT; goto batch_err; }
148 vma = find_vma( current->mm, m.addr );
150 if ( !vma )
151 { ret = -EINVAL; goto batch_err; }
153 if ( m.addr > PAGE_OFFSET )
154 { ret = -EFAULT; goto batch_err; }
156 if ( (m.addr + (m.num<<PAGE_SHIFT)) > vma->vm_end )
157 { ret = -EFAULT; goto batch_err; }
159 p = m.arr;
160 addr = m.addr;
161 for ( i = 0; i < m.num; i++, addr += PAGE_SIZE, p++ )
162 {
163 if ( get_user(mfn, p) )
164 return -EFAULT;
166 ret = create_lookup_pte_addr(vma->vm_mm, addr, &ptep);
167 if (ret)
168 goto batch_err;
170 u.val = pte_val_ma(pfn_pte_ma(mfn, vma->vm_page_prot));
171 u.ptr = ptep;
173 if ( unlikely(HYPERVISOR_mmu_update(&u, 1, NULL, m.dom) < 0) )
174 put_user(0xF0000000 | mfn, p);
175 }
177 ret = 0;
178 break;
180 batch_err:
181 printk("batch_err ret=%d vma=%p addr=%lx num=%d arr=%p %lx-%lx\n",
182 ret, vma, m.addr, m.num, m.arr,
183 vma ? vma->vm_start : 0, vma ? vma->vm_end : 0);
184 break;
185 }
186 break;
187 #endif
189 case IOCTL_PRIVCMD_GET_MACH2PHYS_START_MFN:
190 {
191 unsigned long m2pv = (unsigned long)machine_to_phys_mapping;
192 pgd_t *pgd = pgd_offset_k(m2pv);
193 pud_t *pud = pud_offset(pgd, m2pv);
194 pmd_t *pmd = pmd_offset(pud, m2pv);
195 unsigned long m2p_start_mfn = (*(unsigned long *)pmd) >> PAGE_SHIFT;
196 ret = put_user(m2p_start_mfn, (unsigned long *)data) ? -EFAULT: 0;
197 }
198 break;
200 case IOCTL_PRIVCMD_INITDOMAIN_STORE:
201 {
202 extern int do_xenbus_probe(void*);
203 unsigned long page;
205 if (xen_start_info.store_evtchn != 0) {
206 ret = xen_start_info.store_mfn;
207 break;
208 }
210 /* Allocate page. */
211 page = get_zeroed_page(GFP_KERNEL);
212 if (!page) {
213 ret = -ENOMEM;
214 break;
215 }
217 /* We don't refcnt properly, so set reserved on page.
218 * (this allocation is permanent) */
219 SetPageReserved(virt_to_page(page));
221 /* Initial connect. Setup channel and page. */
222 xen_start_info.store_evtchn = data;
223 xen_start_info.store_mfn = pfn_to_mfn(virt_to_phys((void *)page) >>
224 PAGE_SHIFT);
225 ret = xen_start_info.store_mfn;
227 /* We'll return then this will wait for daemon to answer */
228 kthread_run(do_xenbus_probe, NULL, "xenbus_probe");
229 }
230 break;
232 default:
233 ret = -EINVAL;
234 break;
235 }
236 return ret;
237 }
239 static int privcmd_mmap(struct file * file, struct vm_area_struct * vma)
240 {
241 /* DONTCOPY is essential for Xen as copy_page_range is broken. */
242 vma->vm_flags |= VM_RESERVED | VM_IO | VM_DONTCOPY;
244 return 0;
245 }
247 static struct file_operations privcmd_file_ops = {
248 ioctl : privcmd_ioctl,
249 mmap: privcmd_mmap
250 };
253 static int __init privcmd_init(void)
254 {
255 privcmd_intf = create_xen_proc_entry("privcmd", 0400);
256 if ( privcmd_intf != NULL )
257 privcmd_intf->proc_fops = &privcmd_file_ops;
259 return 0;
260 }
262 __initcall(privcmd_init);