ia64/xen-unstable

view xen/arch/x86/domctl.c @ 13917:d44eb9e7f97b

Another domctl fix to insert missing copy_to_guest.
Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
author kfraser@localhost.localdomain
date Wed Feb 14 15:54:47 2007 +0000 (2007-02-14)
parents 929868cd3715
children f65a24235f58
line source
1 /******************************************************************************
2 * Arch-specific domctl.c
3 *
4 * Copyright (c) 2002-2006, K A Fraser
5 */
7 #include <xen/config.h>
8 #include <xen/types.h>
9 #include <xen/lib.h>
10 #include <xen/mm.h>
11 #include <xen/guest_access.h>
12 #include <xen/compat.h>
13 #include <public/domctl.h>
14 #include <xen/sched.h>
15 #include <xen/domain.h>
16 #include <xen/event.h>
17 #include <xen/domain_page.h>
18 #include <asm/msr.h>
19 #include <xen/trace.h>
20 #include <xen/console.h>
21 #include <xen/iocap.h>
22 #include <asm/paging.h>
23 #include <asm/irq.h>
24 #include <asm/hvm/hvm.h>
25 #include <asm/hvm/support.h>
26 #include <asm/processor.h>
27 #include <public/hvm/e820.h>
29 long arch_do_domctl(
30 struct xen_domctl *domctl,
31 XEN_GUEST_HANDLE(xen_domctl_t) u_domctl)
32 {
33 long ret = 0;
35 switch ( domctl->cmd )
36 {
38 case XEN_DOMCTL_shadow_op:
39 {
40 struct domain *d;
41 ret = -ESRCH;
42 d = get_domain_by_id(domctl->domain);
43 if ( d != NULL )
44 {
45 ret = paging_domctl(d,
46 &domctl->u.shadow_op,
47 guest_handle_cast(u_domctl, void));
48 put_domain(d);
49 copy_to_guest(u_domctl, domctl, 1);
50 }
51 }
52 break;
54 case XEN_DOMCTL_ioport_permission:
55 {
56 struct domain *d;
57 unsigned int fp = domctl->u.ioport_permission.first_port;
58 unsigned int np = domctl->u.ioport_permission.nr_ports;
60 ret = -EINVAL;
61 if ( (fp + np) > 65536 )
62 break;
64 ret = -ESRCH;
65 if ( unlikely((d = get_domain_by_id(domctl->domain)) == NULL) )
66 break;
68 if ( np == 0 )
69 ret = 0;
70 else if ( domctl->u.ioport_permission.allow_access )
71 ret = ioports_permit_access(d, fp, fp + np - 1);
72 else
73 ret = ioports_deny_access(d, fp, fp + np - 1);
75 put_domain(d);
76 }
77 break;
79 case XEN_DOMCTL_getpageframeinfo:
80 {
81 struct page_info *page;
82 unsigned long mfn = domctl->u.getpageframeinfo.gmfn;
83 domid_t dom = domctl->domain;
84 struct domain *d;
86 ret = -EINVAL;
88 if ( unlikely(!mfn_valid(mfn)) ||
89 unlikely((d = get_domain_by_id(dom)) == NULL) )
90 break;
92 page = mfn_to_page(mfn);
94 if ( likely(get_page(page, d)) )
95 {
96 ret = 0;
98 domctl->u.getpageframeinfo.type = XEN_DOMCTL_PFINFO_NOTAB;
100 if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
101 {
102 switch ( page->u.inuse.type_info & PGT_type_mask )
103 {
104 case PGT_l1_page_table:
105 domctl->u.getpageframeinfo.type = XEN_DOMCTL_PFINFO_L1TAB;
106 break;
107 case PGT_l2_page_table:
108 domctl->u.getpageframeinfo.type = XEN_DOMCTL_PFINFO_L2TAB;
109 break;
110 case PGT_l3_page_table:
111 domctl->u.getpageframeinfo.type = XEN_DOMCTL_PFINFO_L3TAB;
112 break;
113 case PGT_l4_page_table:
114 domctl->u.getpageframeinfo.type = XEN_DOMCTL_PFINFO_L4TAB;
115 break;
116 }
117 }
119 put_page(page);
120 }
122 put_domain(d);
124 copy_to_guest(u_domctl, domctl, 1);
125 }
126 break;
128 case XEN_DOMCTL_getpageframeinfo2:
129 {
130 int n,j;
131 int num = domctl->u.getpageframeinfo2.num;
132 domid_t dom = domctl->domain;
133 struct domain *d;
134 uint32_t *arr32;
135 ret = -ESRCH;
137 if ( unlikely((d = get_domain_by_id(dom)) == NULL) )
138 break;
140 if ( unlikely(num > 1024) )
141 {
142 ret = -E2BIG;
143 put_domain(d);
144 break;
145 }
147 arr32 = alloc_xenheap_page();
149 ret = 0;
150 for ( n = 0; n < num; )
151 {
152 int k = PAGE_SIZE / 4;
153 if ( (num - n) < k )
154 k = num - n;
156 if ( copy_from_guest_offset(arr32,
157 domctl->u.getpageframeinfo2.array,
158 n, k) )
159 {
160 ret = -EINVAL;
161 break;
162 }
164 for ( j = 0; j < k; j++ )
165 {
166 struct page_info *page;
167 unsigned long mfn = arr32[j];
169 page = mfn_to_page(mfn);
171 if ( likely(mfn_valid(mfn) && get_page(page, d)) )
172 {
173 unsigned long type = 0;
175 switch( page->u.inuse.type_info & PGT_type_mask )
176 {
177 case PGT_l1_page_table:
178 type = XEN_DOMCTL_PFINFO_L1TAB;
179 break;
180 case PGT_l2_page_table:
181 type = XEN_DOMCTL_PFINFO_L2TAB;
182 break;
183 case PGT_l3_page_table:
184 type = XEN_DOMCTL_PFINFO_L3TAB;
185 break;
186 case PGT_l4_page_table:
187 type = XEN_DOMCTL_PFINFO_L4TAB;
188 break;
189 }
191 if ( page->u.inuse.type_info & PGT_pinned )
192 type |= XEN_DOMCTL_PFINFO_LPINTAB;
193 arr32[j] |= type;
194 put_page(page);
195 }
196 else
197 arr32[j] |= XEN_DOMCTL_PFINFO_XTAB;
199 }
201 if ( copy_to_guest_offset(domctl->u.getpageframeinfo2.array,
202 n, arr32, k) )
203 {
204 ret = -EINVAL;
205 break;
206 }
208 n += k;
209 }
211 free_xenheap_page(arr32);
213 put_domain(d);
214 }
215 break;
217 case XEN_DOMCTL_getmemlist:
218 {
219 int i;
220 struct domain *d = get_domain_by_id(domctl->domain);
221 unsigned long max_pfns = domctl->u.getmemlist.max_pfns;
222 uint64_t mfn;
223 struct list_head *list_ent;
225 ret = -EINVAL;
226 if ( d != NULL )
227 {
228 ret = 0;
230 spin_lock(&d->page_alloc_lock);
232 list_ent = d->page_list.next;
233 for ( i = 0; (i < max_pfns) && (list_ent != &d->page_list); i++ )
234 {
235 mfn = page_to_mfn(list_entry(
236 list_ent, struct page_info, list));
237 if ( copy_to_guest_offset(domctl->u.getmemlist.buffer,
238 i, &mfn, 1) )
239 {
240 ret = -EFAULT;
241 break;
242 }
243 list_ent = mfn_to_page(mfn)->list.next;
244 }
246 spin_unlock(&d->page_alloc_lock);
248 domctl->u.getmemlist.num_pfns = i;
249 copy_to_guest(u_domctl, domctl, 1);
251 put_domain(d);
252 }
253 }
254 break;
256 case XEN_DOMCTL_hypercall_init:
257 {
258 struct domain *d = get_domain_by_id(domctl->domain);
259 unsigned long gmfn = domctl->u.hypercall_init.gmfn;
260 unsigned long mfn;
261 void *hypercall_page;
263 ret = -ESRCH;
264 if ( unlikely(d == NULL) )
265 break;
267 mfn = gmfn_to_mfn(d, gmfn);
269 ret = -EACCES;
270 if ( !mfn_valid(mfn) ||
271 !get_page_and_type(mfn_to_page(mfn), d, PGT_writable_page) )
272 {
273 put_domain(d);
274 break;
275 }
277 ret = 0;
279 hypercall_page = map_domain_page(mfn);
280 hypercall_page_initialise(d, hypercall_page);
281 unmap_domain_page(hypercall_page);
283 put_page_and_type(mfn_to_page(mfn));
285 put_domain(d);
286 }
287 break;
289 case XEN_DOMCTL_sethvmcontext:
290 {
291 struct hvm_domain_context c;
292 struct domain *d;
294 c.cur = 0;
295 c.size = domctl->u.hvmcontext.size;
296 c.data = NULL;
298 ret = -ESRCH;
299 if ( (d = get_domain_by_id(domctl->domain)) == NULL )
300 break;
302 ret = -EINVAL;
303 if ( !is_hvm_domain(d) )
304 goto sethvmcontext_out;
306 ret = -ENOMEM;
307 if ( (c.data = xmalloc_bytes(c.size)) == NULL )
308 goto sethvmcontext_out;
310 ret = -EFAULT;
311 if ( copy_from_guest(c.data, domctl->u.hvmcontext.buffer, c.size) != 0)
312 goto sethvmcontext_out;
314 ret = hvm_load(d, &c);
316 sethvmcontext_out:
317 if ( c.data != NULL )
318 xfree(c.data);
320 put_domain(d);
321 }
322 break;
324 case XEN_DOMCTL_gethvmcontext:
325 {
326 struct hvm_domain_context c;
327 struct domain *d;
329 ret = -ESRCH;
330 if ( (d = get_domain_by_id(domctl->domain)) == NULL )
331 break;
333 ret = -EINVAL;
334 if ( !is_hvm_domain(d) )
335 goto gethvmcontext_out;
337 c.cur = 0;
338 c.size = hvm_save_size(d);
339 c.data = NULL;
341 if ( guest_handle_is_null(domctl->u.hvmcontext.buffer) )
342 {
343 /* Client is querying for the correct buffer size */
344 domctl->u.hvmcontext.size = c.size;
345 ret = 0;
346 goto gethvmcontext_out;
347 }
349 /* Check that the client has a big enough buffer */
350 ret = -ENOSPC;
351 if ( domctl->u.hvmcontext.size < c.size )
352 goto gethvmcontext_out;
354 /* Allocate our own marshalling buffer */
355 ret = -ENOMEM;
356 if ( (c.data = xmalloc_bytes(c.size)) == NULL )
357 goto gethvmcontext_out;
359 ret = hvm_save(d, &c);
361 domctl->u.hvmcontext.size = c.cur;
362 if ( copy_to_guest(domctl->u.hvmcontext.buffer, c.data, c.size) != 0 )
363 ret = -EFAULT;
365 gethvmcontext_out:
366 if ( copy_to_guest(u_domctl, domctl, 1) )
367 ret = -EFAULT;
369 if ( c.data != NULL )
370 xfree(c.data);
372 put_domain(d);
373 }
374 break;
376 case XEN_DOMCTL_set_address_size:
377 {
378 struct domain *d;
380 ret = -ESRCH;
381 if ( (d = get_domain_by_id(domctl->domain)) == NULL )
382 break;
384 switch ( domctl->u.address_size.size )
385 {
386 #ifdef CONFIG_COMPAT
387 case 32:
388 ret = switch_compat(d);
389 break;
390 case 64:
391 ret = switch_native(d);
392 break;
393 #endif
394 default:
395 ret = (domctl->u.address_size.size == BITS_PER_LONG) ? 0 : -EINVAL;
396 break;
397 }
399 put_domain(d);
400 }
401 break;
403 case XEN_DOMCTL_get_address_size:
404 {
405 struct domain *d;
407 ret = -ESRCH;
408 if ( (d = get_domain_by_id(domctl->domain)) == NULL )
409 break;
411 domctl->u.address_size.size = BITS_PER_GUEST_LONG(d);
413 ret = 0;
414 put_domain(d);
416 if ( copy_to_guest(u_domctl, domctl, 1) )
417 ret = -EFAULT;
418 }
419 break;
421 default:
422 ret = -ENOSYS;
423 break;
424 }
426 return ret;
427 }
429 void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
430 {
431 #ifdef CONFIG_COMPAT
432 #define c(fld) (!IS_COMPAT(v->domain) ? (c.nat->fld) : (c.cmp->fld))
433 #else
434 #define c(fld) (c.nat->fld)
435 #endif
437 if ( !IS_COMPAT(v->domain) )
438 memcpy(c.nat, &v->arch.guest_context, sizeof(*c.nat));
439 #ifdef CONFIG_COMPAT
440 else
441 XLAT_vcpu_guest_context(c.cmp, &v->arch.guest_context);
442 #endif
444 if ( is_hvm_vcpu(v) )
445 {
446 if ( !IS_COMPAT(v->domain) )
447 hvm_store_cpu_guest_regs(v, &c.nat->user_regs, c.nat->ctrlreg);
448 #ifdef CONFIG_COMPAT
449 else
450 {
451 struct cpu_user_regs user_regs;
452 typeof(c.nat->ctrlreg) ctrlreg;
453 unsigned i;
455 hvm_store_cpu_guest_regs(v, &user_regs, ctrlreg);
456 XLAT_cpu_user_regs(&c.cmp->user_regs, &user_regs);
457 for ( i = 0; i < ARRAY_SIZE(c.cmp->ctrlreg); ++i )
458 c.cmp->ctrlreg[i] = ctrlreg[i];
459 }
460 #endif
461 }
462 else
463 {
464 /* IOPL privileges are virtualised: merge back into returned eflags. */
465 BUG_ON((c(user_regs.eflags) & EF_IOPL) != 0);
466 c(user_regs.eflags |= v->arch.iopl << 12);
467 }
469 c(flags &= ~(VGCF_i387_valid|VGCF_in_kernel));
470 if ( test_bit(_VCPUF_fpu_initialised, &v->vcpu_flags) )
471 c(flags |= VGCF_i387_valid);
472 if ( guest_kernel_mode(v, &v->arch.guest_context.user_regs) )
473 c(flags |= VGCF_in_kernel);
475 if ( !IS_COMPAT(v->domain) )
476 c.nat->ctrlreg[3] = xen_pfn_to_cr3(pagetable_get_pfn(v->arch.guest_table));
477 #ifdef CONFIG_COMPAT
478 else
479 {
480 l4_pgentry_t *l4e = __va(pagetable_get_paddr(v->arch.guest_table));
481 c.cmp->ctrlreg[3] = compat_pfn_to_cr3(l4e_get_pfn(*l4e));
482 }
483 #endif
485 c(vm_assist = v->domain->vm_assist);
486 #undef c
487 }
489 /*
490 * Local variables:
491 * mode: C
492 * c-set-style: "BSD"
493 * c-basic-offset: 4
494 * tab-width: 4
495 * indent-tabs-mode: nil
496 * End:
497 */