ia64/xen-unstable

annotate xen/common/memory.c @ 13295:244e46e7d021

Enable compatibility mode operation for HYPERVISOR_memory_op,
HYPERVISOR_update_descriptor, HYPERVISOR_update_va_mapping. This also
introduces infrastructure to do argument translation.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Emmanuel Ackaouy <ack@xensource.com>
date Fri Jan 05 17:34:31 2007 +0000 (2007-01-05)
parents 39e40ccf7df5
children 271ffb1c12eb
rev   line source
kaf24@6486 1 /******************************************************************************
kaf24@6486 2 * memory.c
kaf24@6486 3 *
kaf24@6486 4 * Code to handle memory-related requests.
kaf24@6486 5 *
kaf24@6486 6 * Copyright (c) 2003-2004, B Dragovic
kaf24@6486 7 * Copyright (c) 2003-2005, K A Fraser
kaf24@6486 8 */
kaf24@6486 9
kaf24@6486 10 #include <xen/config.h>
kaf24@6486 11 #include <xen/types.h>
kaf24@6486 12 #include <xen/lib.h>
kaf24@6486 13 #include <xen/mm.h>
kaf24@6486 14 #include <xen/perfc.h>
kaf24@6486 15 #include <xen/sched.h>
kaf24@6486 16 #include <xen/event.h>
kaf24@6486 17 #include <xen/shadow.h>
kaf24@8468 18 #include <xen/iocap.h>
kaf24@9068 19 #include <xen/guest_access.h>
ack@13295 20 #include <xen/hypercall.h>
kaf24@11219 21 #include <xen/errno.h>
kaf24@6486 22 #include <asm/current.h>
kaf24@6486 23 #include <asm/hardirq.h>
kaf24@6486 24 #include <public/memory.h>
kaf24@6486 25
kfraser@12374 26 struct memop_args {
kfraser@12374 27 /* INPUT */
kfraser@12374 28 struct domain *domain; /* Domain to be affected. */
kfraser@12374 29 XEN_GUEST_HANDLE(xen_pfn_t) extent_list; /* List of extent base addrs. */
kfraser@12374 30 unsigned int nr_extents; /* Number of extents to allocate or free. */
kfraser@12374 31 unsigned int extent_order; /* Size of each extent. */
kfraser@12374 32 unsigned int memflags; /* Allocation flags. */
kfraser@12374 33
kfraser@12374 34 /* INPUT/OUTPUT */
kfraser@12374 35 unsigned int nr_done; /* Number of extents processed so far. */
kfraser@12374 36 int preempted; /* Was the hypercall preempted? */
kfraser@12374 37 };
kfraser@12374 38
kfraser@12374 39 static unsigned int select_local_cpu(struct domain *d)
kfraser@12374 40 {
kfraser@12374 41 struct vcpu *v = d->vcpu[0];
kfraser@12374 42 return (v ? v->processor : 0);
kfraser@12374 43 }
kfraser@12374 44
kfraser@12374 45 static void increase_reservation(struct memop_args *a)
kaf24@6486 46 {
kaf24@8726 47 struct page_info *page;
kaf24@10314 48 unsigned long i;
kaf24@10314 49 xen_pfn_t mfn;
kfraser@12374 50 struct domain *d = a->domain;
kfraser@12374 51 unsigned int cpu = select_local_cpu(d);
kaf24@6486 52
kfraser@12374 53 if ( !guest_handle_is_null(a->extent_list) &&
kfraser@12374 54 !guest_handle_okay(a->extent_list, a->nr_extents) )
kfraser@12374 55 return;
kaf24@6486 56
kfraser@12374 57 if ( (a->extent_order != 0) &&
kaf24@8468 58 !multipage_allocation_permitted(current->domain) )
kfraser@12374 59 return;
kaf24@6486 60
kfraser@12374 61 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@6486 62 {
kaf24@6486 63 if ( hypercall_preempt_check() )
kaf24@6607 64 {
kfraser@12374 65 a->preempted = 1;
kfraser@12374 66 goto out;
kaf24@6607 67 }
kaf24@6486 68
kfraser@12374 69 page = __alloc_domheap_pages(d, cpu, a->extent_order, a->memflags);
kfraser@12374 70 if ( unlikely(page == NULL) )
kaf24@6486 71 {
kaf24@12038 72 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
kfraser@10418 73 "id=%d memflags=%x (%ld of %d)\n",
kfraser@12374 74 a->extent_order, d->domain_id, a->memflags,
kfraser@12374 75 i, a->nr_extents);
kfraser@12374 76 goto out;
kaf24@6486 77 }
kaf24@6486 78
kaf24@6486 79 /* Inform the domain of the new page's machine address. */
kfraser@12374 80 if ( !guest_handle_is_null(a->extent_list) )
kaf24@8859 81 {
kaf24@8859 82 mfn = page_to_mfn(page);
kfraser@12374 83 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
kfraser@12374 84 goto out;
kaf24@8859 85 }
kaf24@6486 86 }
kaf24@6486 87
kfraser@12374 88 out:
kfraser@12374 89 a->nr_done = i;
kaf24@6486 90 }
sos22@8688 91
kfraser@12374 92 static void populate_physmap(struct memop_args *a)
kaf24@8673 93 {
kaf24@8726 94 struct page_info *page;
kaf24@10314 95 unsigned long i, j;
kfraser@12374 96 xen_pfn_t gpfn, mfn;
kfraser@12374 97 struct domain *d = a->domain;
kfraser@12374 98 unsigned int cpu = select_local_cpu(d);
kaf24@8673 99
kfraser@12374 100 if ( !guest_handle_okay(a->extent_list, a->nr_extents) )
kfraser@12374 101 return;
kaf24@8673 102
kfraser@12374 103 if ( (a->extent_order != 0) &&
kaf24@8673 104 !multipage_allocation_permitted(current->domain) )
kfraser@12374 105 return;
kaf24@8673 106
kfraser@12374 107 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@8673 108 {
kaf24@8673 109 if ( hypercall_preempt_check() )
kaf24@8673 110 {
kfraser@12374 111 a->preempted = 1;
sos22@8688 112 goto out;
kaf24@8673 113 }
kaf24@8673 114
kfraser@12374 115 if ( unlikely(__copy_from_guest_offset(&gpfn, a->extent_list, i, 1)) )
kaf24@8859 116 goto out;
kaf24@8859 117
kfraser@12374 118 page = __alloc_domheap_pages(d, cpu, a->extent_order, a->memflags);
kfraser@12374 119 if ( unlikely(page == NULL) )
kaf24@8673 120 {
kaf24@12038 121 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
kfraser@12374 122 "id=%d memflags=%x (%ld of %d)\n",
kfraser@12374 123 a->extent_order, d->domain_id, a->memflags,
kfraser@12374 124 i, a->nr_extents);
sos22@8688 125 goto out;
kaf24@8673 126 }
kaf24@8673 127
kaf24@8726 128 mfn = page_to_mfn(page);
kaf24@8673 129
kaf24@8694 130 if ( unlikely(shadow_mode_translate(d)) )
kaf24@8694 131 {
kfraser@12374 132 for ( j = 0; j < (1 << a->extent_order); j++ )
kaf24@8736 133 guest_physmap_add_page(d, gpfn + j, mfn + j);
sos22@8688 134 }
kaf24@8694 135 else
kaf24@8694 136 {
kfraser@12374 137 for ( j = 0; j < (1 << a->extent_order); j++ )
kaf24@8736 138 set_gpfn_from_mfn(mfn + j, gpfn + j);
kaf24@8673 139
sos22@8688 140 /* Inform the domain of the new page's machine address. */
kfraser@12374 141 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
sos22@8688 142 goto out;
sos22@8688 143 }
kaf24@8673 144 }
kaf24@8673 145
sos22@8688 146 out:
kfraser@12374 147 a->nr_done = i;
kaf24@8673 148 }
cl349@9211 149
kfraser@12374 150 int guest_remove_page(struct domain *d, unsigned long gmfn)
cl349@9211 151 {
cl349@9211 152 struct page_info *page;
cl349@9211 153 unsigned long mfn;
cl349@9211 154
cl349@9211 155 mfn = gmfn_to_mfn(d, gmfn);
cl349@9211 156 if ( unlikely(!mfn_valid(mfn)) )
cl349@9211 157 {
kaf24@12038 158 gdprintk(XENLOG_INFO, "Domain %u page number %lx invalid\n",
tdeegan@11172 159 d->domain_id, gmfn);
cl349@9211 160 return 0;
cl349@9211 161 }
cl349@9211 162
cl349@9211 163 page = mfn_to_page(mfn);
cl349@9211 164 if ( unlikely(!get_page(page, d)) )
cl349@9211 165 {
kaf24@12038 166 gdprintk(XENLOG_INFO, "Bad page free for domain %u\n", d->domain_id);
cl349@9211 167 return 0;
cl349@9211 168 }
cl349@9211 169
cl349@9211 170 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
cl349@9211 171 put_page_and_type(page);
cl349@9211 172
cl349@9211 173 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
cl349@9211 174 put_page(page);
cl349@9211 175
kfraser@10823 176 if ( unlikely(!page_is_removable(page)) )
kfraser@10584 177 {
kfraser@10584 178 /* We'll make this a guest-visible error in future, so take heed! */
kaf24@12038 179 gdprintk(XENLOG_INFO, "Dom%d freeing in-use page %lx (pseudophys %lx):"
kaf24@10744 180 " count=%lx type=%lx\n",
kfraser@10584 181 d->domain_id, mfn, get_gpfn_from_mfn(mfn),
kaf24@10744 182 (unsigned long)page->count_info, page->u.inuse.type_info);
kfraser@10584 183 }
kfraser@10584 184
kfraser@11212 185 guest_physmap_remove_page(d, gmfn, mfn);
cl349@9211 186
cl349@9211 187 put_page(page);
cl349@9211 188
cl349@9211 189 return 1;
cl349@9211 190 }
cl349@9211 191
kfraser@12374 192 static void decrease_reservation(struct memop_args *a)
kaf24@6486 193 {
kaf24@10314 194 unsigned long i, j;
kaf24@10314 195 xen_pfn_t gmfn;
kaf24@6486 196
kfraser@12374 197 if ( !guest_handle_okay(a->extent_list, a->nr_extents) )
kfraser@12374 198 return;
kaf24@6486 199
kfraser@12374 200 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@6486 201 {
kaf24@6486 202 if ( hypercall_preempt_check() )
kaf24@6607 203 {
kfraser@12374 204 a->preempted = 1;
kfraser@12374 205 goto out;
kaf24@6607 206 }
kaf24@6486 207
kfraser@12374 208 if ( unlikely(__copy_from_guest_offset(&gmfn, a->extent_list, i, 1)) )
kfraser@12374 209 goto out;
kaf24@6486 210
kfraser@12374 211 for ( j = 0; j < (1 << a->extent_order); j++ )
kfraser@12374 212 if ( !guest_remove_page(a->domain, gmfn + j) )
kfraser@12374 213 goto out;
kaf24@6486 214 }
kaf24@6486 215
kfraser@12374 216 out:
kfraser@12374 217 a->nr_done = i;
kaf24@6486 218 }
kaf24@6486 219
kfraser@12374 220 static long translate_gpfn_list(
kaf24@9873 221 XEN_GUEST_HANDLE(xen_translate_gpfn_list_t) uop, unsigned long *progress)
kaf24@8871 222 {
kaf24@8871 223 struct xen_translate_gpfn_list op;
kaf24@10314 224 unsigned long i;
kaf24@10314 225 xen_pfn_t gpfn;
kaf24@10314 226 xen_pfn_t mfn;
kaf24@8871 227 struct domain *d;
kaf24@6486 228
kaf24@9068 229 if ( copy_from_guest(&op, uop, 1) )
kaf24@8871 230 return -EFAULT;
kaf24@8871 231
kaf24@8871 232 /* Is size too large for us to encode a continuation? */
ack@13295 233 if ( op.nr_gpfns > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kaf24@8871 234 return -EINVAL;
kaf24@8871 235
kaf24@9068 236 if ( !guest_handle_okay(op.gpfn_list, op.nr_gpfns) ||
kaf24@9068 237 !guest_handle_okay(op.mfn_list, op.nr_gpfns) )
kaf24@8871 238 return -EFAULT;
kaf24@8871 239
kaf24@8871 240 if ( op.domid == DOMID_SELF )
kaf24@8871 241 op.domid = current->domain->domain_id;
kaf24@8871 242 else if ( !IS_PRIV(current->domain) )
kaf24@8871 243 return -EPERM;
kaf24@8871 244
kaf24@8871 245 if ( (d = find_domain_by_id(op.domid)) == NULL )
kaf24@8871 246 return -ESRCH;
kaf24@8871 247
kfraser@11212 248 if ( !shadow_mode_translate(d) )
kaf24@8871 249 {
kaf24@8871 250 put_domain(d);
kaf24@8871 251 return -EINVAL;
kaf24@8871 252 }
kaf24@8871 253
kaf24@8871 254 for ( i = *progress; i < op.nr_gpfns; i++ )
kaf24@8871 255 {
kaf24@8871 256 if ( hypercall_preempt_check() )
kaf24@8871 257 {
kaf24@8871 258 put_domain(d);
kaf24@8871 259 *progress = i;
kaf24@8871 260 return -EAGAIN;
kaf24@8871 261 }
kaf24@8871 262
kaf24@9068 263 if ( unlikely(__copy_from_guest_offset(&gpfn, op.gpfn_list, i, 1)) )
kaf24@8871 264 {
kaf24@8871 265 put_domain(d);
kaf24@8871 266 return -EFAULT;
kaf24@8871 267 }
kaf24@8871 268
kaf24@8871 269 mfn = gmfn_to_mfn(d, gpfn);
kaf24@8871 270
kaf24@9068 271 if ( unlikely(__copy_to_guest_offset(op.mfn_list, i, &mfn, 1)) )
kaf24@8871 272 {
kaf24@8871 273 put_domain(d);
kaf24@8871 274 return -EFAULT;
kaf24@8871 275 }
kaf24@8871 276 }
kaf24@8871 277
kaf24@8871 278 put_domain(d);
kaf24@8871 279 return 0;
kaf24@8871 280 }
kaf24@8871 281
kfraser@12374 282 static long memory_exchange(XEN_GUEST_HANDLE(xen_memory_exchange_t) arg)
kfraser@10418 283 {
kfraser@10418 284 struct xen_memory_exchange exch;
kfraser@10418 285 LIST_HEAD(in_chunk_list);
kfraser@10418 286 LIST_HEAD(out_chunk_list);
kfraser@10418 287 unsigned long in_chunk_order, out_chunk_order;
kaf24@10459 288 xen_pfn_t gpfn, gmfn, mfn;
kfraser@10418 289 unsigned long i, j, k;
kfraser@11973 290 unsigned int memflags = 0, cpu;
kfraser@10418 291 long rc = 0;
kfraser@10418 292 struct domain *d;
kfraser@10418 293 struct page_info *page;
kfraser@10418 294
kfraser@10418 295 if ( copy_from_guest(&exch, arg, 1) )
kfraser@10418 296 return -EFAULT;
kfraser@10418 297
kfraser@10418 298 /* Various sanity checks. */
kfraser@10418 299 if ( (exch.nr_exchanged > exch.in.nr_extents) ||
kfraser@10418 300 /* Input and output domain identifiers match? */
kfraser@10418 301 (exch.in.domid != exch.out.domid) ||
kfraser@10418 302 /* Sizes of input and output lists do not overflow a long? */
kfraser@10418 303 ((~0UL >> exch.in.extent_order) < exch.in.nr_extents) ||
kfraser@10418 304 ((~0UL >> exch.out.extent_order) < exch.out.nr_extents) ||
kfraser@10418 305 /* Sizes of input and output lists match? */
kfraser@10418 306 ((exch.in.nr_extents << exch.in.extent_order) !=
kfraser@10418 307 (exch.out.nr_extents << exch.out.extent_order)) )
kfraser@10418 308 {
kfraser@10418 309 rc = -EINVAL;
kfraser@10418 310 goto fail_early;
kfraser@10418 311 }
kfraser@10418 312
kfraser@10418 313 /* Only privileged guests can allocate multi-page contiguous extents. */
kfraser@10418 314 if ( ((exch.in.extent_order != 0) || (exch.out.extent_order != 0)) &&
kfraser@10418 315 !multipage_allocation_permitted(current->domain) )
kfraser@10418 316 {
kfraser@10418 317 rc = -EPERM;
kfraser@10418 318 goto fail_early;
kfraser@10418 319 }
kfraser@10418 320
kfraser@10418 321 if ( (exch.out.address_bits != 0) &&
kfraser@10418 322 (exch.out.address_bits <
kfraser@10418 323 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kfraser@10418 324 {
kfraser@12641 325 if ( exch.out.address_bits < dma_bitsize )
kfraser@10418 326 {
kfraser@10418 327 rc = -ENOMEM;
kfraser@10418 328 goto fail_early;
kfraser@10418 329 }
kfraser@10418 330 memflags = MEMF_dma;
kfraser@10418 331 }
kfraser@10418 332
kfraser@10418 333 if ( exch.in.extent_order <= exch.out.extent_order )
kfraser@10418 334 {
kfraser@10418 335 in_chunk_order = exch.out.extent_order - exch.in.extent_order;
kfraser@10418 336 out_chunk_order = 0;
kfraser@10418 337 }
kfraser@10418 338 else
kfraser@10418 339 {
kfraser@10418 340 in_chunk_order = 0;
kfraser@10418 341 out_chunk_order = exch.in.extent_order - exch.out.extent_order;
kfraser@10418 342 }
kfraser@10418 343
kfraser@10418 344 /*
kfraser@10418 345 * Only support exchange on calling domain right now. Otherwise there are
kfraser@10418 346 * tricky corner cases to consider (e.g., DOMF_dying domain).
kfraser@10418 347 */
kfraser@10418 348 if ( unlikely(exch.in.domid != DOMID_SELF) )
kfraser@10418 349 {
kfraser@10418 350 rc = IS_PRIV(current->domain) ? -EINVAL : -EPERM;
kfraser@10418 351 goto fail_early;
kfraser@10418 352 }
kfraser@10418 353 d = current->domain;
kfraser@10418 354
kfraser@12374 355 cpu = select_local_cpu(d);
kfraser@11973 356
kfraser@12374 357 for ( i = (exch.nr_exchanged >> in_chunk_order);
kfraser@12374 358 i < (exch.in.nr_extents >> in_chunk_order);
kfraser@12374 359 i++ )
kfraser@10418 360 {
kfraser@10418 361 if ( hypercall_preempt_check() )
kfraser@10418 362 {
kfraser@12374 363 exch.nr_exchanged = i << in_chunk_order;
kfraser@10418 364 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 365 return -EFAULT;
kfraser@10418 366 return hypercall_create_continuation(
kfraser@10418 367 __HYPERVISOR_memory_op, "lh", XENMEM_exchange, arg);
kfraser@10418 368 }
kfraser@10418 369
kfraser@10418 370 /* Steal a chunk's worth of input pages from the domain. */
kfraser@10418 371 for ( j = 0; j < (1UL << in_chunk_order); j++ )
kfraser@10418 372 {
kfraser@10418 373 if ( unlikely(__copy_from_guest_offset(
kfraser@10418 374 &gmfn, exch.in.extent_start, (i<<in_chunk_order)+j, 1)) )
kfraser@10418 375 {
kfraser@10418 376 rc = -EFAULT;
kfraser@10418 377 goto fail;
kfraser@10418 378 }
kfraser@10418 379
kfraser@10418 380 for ( k = 0; k < (1UL << exch.in.extent_order); k++ )
kfraser@10418 381 {
kfraser@10418 382 mfn = gmfn_to_mfn(d, gmfn + k);
kfraser@10418 383 if ( unlikely(!mfn_valid(mfn)) )
kfraser@10418 384 {
kfraser@10418 385 rc = -EINVAL;
kfraser@10418 386 goto fail;
kfraser@10418 387 }
kfraser@10418 388
kfraser@10418 389 page = mfn_to_page(mfn);
kfraser@10418 390
kfraser@10418 391 if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
kfraser@10418 392 {
kfraser@10418 393 rc = -EINVAL;
kfraser@10418 394 goto fail;
kfraser@10418 395 }
kfraser@10418 396
kfraser@10418 397 list_add(&page->list, &in_chunk_list);
kfraser@10418 398 }
kfraser@10418 399 }
kfraser@10418 400
kfraser@10418 401 /* Allocate a chunk's worth of anonymous output pages. */
kfraser@10418 402 for ( j = 0; j < (1UL << out_chunk_order); j++ )
kfraser@10418 403 {
kfraser@12374 404 page = __alloc_domheap_pages(
kfraser@12374 405 NULL, cpu, exch.out.extent_order, memflags);
kfraser@10418 406 if ( unlikely(page == NULL) )
kfraser@10418 407 {
kfraser@10418 408 rc = -ENOMEM;
kfraser@10418 409 goto fail;
kfraser@10418 410 }
kfraser@10418 411
kfraser@10418 412 list_add(&page->list, &out_chunk_list);
kfraser@10418 413 }
kfraser@10418 414
kfraser@10418 415 /*
kfraser@10418 416 * Success! Beyond this point we cannot fail for this chunk.
kfraser@10418 417 */
kfraser@10418 418
kfraser@10418 419 /* Destroy final reference to each input page. */
kfraser@10418 420 while ( !list_empty(&in_chunk_list) )
kfraser@10418 421 {
kfraser@10418 422 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 423 list_del(&page->list);
kfraser@10418 424 if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
kfraser@10418 425 BUG();
kfraser@10418 426 mfn = page_to_mfn(page);
kfraser@10418 427 guest_physmap_remove_page(d, mfn_to_gmfn(d, mfn), mfn);
kfraser@10418 428 put_page(page);
kfraser@10418 429 }
kfraser@10418 430
kfraser@10418 431 /* Assign each output page to the domain. */
kfraser@10418 432 j = 0;
kfraser@10418 433 while ( !list_empty(&out_chunk_list) )
kfraser@10418 434 {
kfraser@10418 435 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 436 list_del(&page->list);
kfraser@10418 437 if ( assign_pages(d, page, exch.out.extent_order,
kfraser@10418 438 MEMF_no_refcount) )
kfraser@10418 439 BUG();
kfraser@10418 440
kfraser@10418 441 /* Note that we ignore errors accessing the output extent list. */
kfraser@10418 442 (void)__copy_from_guest_offset(
kfraser@10418 443 &gpfn, exch.out.extent_start, (i<<out_chunk_order)+j, 1);
kfraser@10418 444
kfraser@10418 445 mfn = page_to_mfn(page);
kfraser@10418 446 if ( unlikely(shadow_mode_translate(d)) )
kfraser@10418 447 {
kfraser@10418 448 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
kfraser@10418 449 guest_physmap_add_page(d, gpfn + k, mfn + k);
kfraser@10418 450 }
kfraser@10418 451 else
kfraser@10418 452 {
kfraser@10418 453 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
kfraser@10418 454 set_gpfn_from_mfn(mfn + k, gpfn + k);
kfraser@10418 455 (void)__copy_to_guest_offset(
kfraser@10418 456 exch.out.extent_start, (i<<out_chunk_order)+j, &mfn, 1);
kfraser@10418 457 }
kfraser@10418 458
kfraser@10418 459 j++;
kfraser@10418 460 }
kfraser@10418 461 BUG_ON(j != (1UL << out_chunk_order));
kfraser@10418 462 }
kfraser@10418 463
kfraser@12374 464 exch.nr_exchanged = exch.in.nr_extents;
kfraser@10418 465 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 466 rc = -EFAULT;
kfraser@10418 467 return rc;
kfraser@10418 468
kfraser@10418 469 /*
kfraser@10418 470 * Failed a chunk! Free any partial chunk work. Tell caller how many
kfraser@10418 471 * chunks succeeded.
kfraser@10418 472 */
kfraser@10418 473 fail:
kfraser@10418 474 /* Reassign any input pages we managed to steal. */
kfraser@10418 475 while ( !list_empty(&in_chunk_list) )
kfraser@10418 476 {
kfraser@10418 477 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 478 list_del(&page->list);
kfraser@10418 479 if ( assign_pages(d, page, 0, MEMF_no_refcount) )
kfraser@10418 480 BUG();
kfraser@10418 481 }
kfraser@10418 482
kfraser@10418 483 /* Free any output pages we managed to allocate. */
kfraser@10418 484 while ( !list_empty(&out_chunk_list) )
kfraser@10418 485 {
kfraser@10418 486 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 487 list_del(&page->list);
kfraser@10418 488 free_domheap_pages(page, exch.out.extent_order);
kfraser@10418 489 }
kfraser@10418 490
kfraser@12374 491 exch.nr_exchanged = i << in_chunk_order;
kfraser@10418 492
kfraser@10418 493 fail_early:
kfraser@10418 494 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 495 rc = -EFAULT;
kfraser@10418 496 return rc;
kfraser@10418 497 }
kfraser@10418 498
kaf24@9873 499 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE(void) arg)
kaf24@6486 500 {
kaf24@6486 501 struct domain *d;
kfraser@12374 502 int rc, op;
kaf24@8871 503 unsigned long start_extent, progress;
kaf24@6486 504 struct xen_memory_reservation reservation;
kfraser@12374 505 struct memop_args args;
kaf24@7959 506 domid_t domid;
kaf24@6486 507
ack@13295 508 op = cmd & MEMOP_CMD_MASK;
kaf24@6486 509
kaf24@6486 510 switch ( op )
kaf24@6486 511 {
kaf24@6486 512 case XENMEM_increase_reservation:
kaf24@6486 513 case XENMEM_decrease_reservation:
kaf24@8673 514 case XENMEM_populate_physmap:
ack@13295 515 start_extent = cmd >> MEMOP_EXTENT_SHIFT;
kfraser@10418 516
kaf24@9068 517 if ( copy_from_guest(&reservation, arg, 1) )
kfraser@10418 518 return start_extent;
kaf24@6486 519
kaf24@8871 520 /* Is size too large for us to encode a continuation? */
ack@13295 521 if ( reservation.nr_extents > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kfraser@10418 522 return start_extent;
kaf24@8871 523
kaf24@6486 524 if ( unlikely(start_extent > reservation.nr_extents) )
kfraser@10418 525 return start_extent;
kaf24@9068 526
kfraser@12374 527 args.extent_list = reservation.extent_start;
kfraser@12374 528 args.nr_extents = reservation.nr_extents;
kfraser@12374 529 args.extent_order = reservation.extent_order;
kfraser@12374 530 args.nr_done = start_extent;
kfraser@12374 531 args.preempted = 0;
kfraser@12374 532 args.memflags = 0;
kaf24@6486 533
kaf24@6701 534 if ( (reservation.address_bits != 0) &&
kaf24@6702 535 (reservation.address_bits <
kaf24@6702 536 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kaf24@6486 537 {
kfraser@12641 538 if ( reservation.address_bits < dma_bitsize )
kfraser@10418 539 return start_extent;
kfraser@12374 540 args.memflags = MEMF_dma;
kaf24@6486 541 }
kaf24@6486 542
kaf24@6486 543 if ( likely(reservation.domid == DOMID_SELF) )
kaf24@6486 544 d = current->domain;
kfraser@10418 545 else if ( !IS_PRIV(current->domain) ||
kfraser@10418 546 ((d = find_domain_by_id(reservation.domid)) == NULL) )
kfraser@10418 547 return start_extent;
kfraser@12374 548 args.domain = d;
kaf24@6486 549
kaf24@8673 550 switch ( op )
kaf24@8673 551 {
kaf24@8673 552 case XENMEM_increase_reservation:
kfraser@12374 553 increase_reservation(&args);
kaf24@8673 554 break;
kaf24@8673 555 case XENMEM_decrease_reservation:
kfraser@12374 556 decrease_reservation(&args);
kaf24@8673 557 break;
kfraser@12374 558 default: /* XENMEM_populate_physmap */
kfraser@12374 559 populate_physmap(&args);
kaf24@8673 560 break;
kaf24@8673 561 }
kaf24@6486 562
kaf24@6486 563 if ( unlikely(reservation.domid != DOMID_SELF) )
kaf24@6486 564 put_domain(d);
kaf24@6486 565
kfraser@12374 566 rc = args.nr_done;
kaf24@6486 567
kfraser@12374 568 if ( args.preempted )
kaf24@9068 569 return hypercall_create_continuation(
kaf24@9068 570 __HYPERVISOR_memory_op, "lh",
ack@13295 571 op | (rc << MEMOP_EXTENT_SHIFT), arg);
kaf24@6607 572
kaf24@6486 573 break;
kaf24@6486 574
kfraser@10418 575 case XENMEM_exchange:
kfraser@10418 576 rc = memory_exchange(guest_handle_cast(arg, xen_memory_exchange_t));
kfraser@10418 577 break;
kfraser@10418 578
kaf24@6486 579 case XENMEM_maximum_ram_page:
kaf24@7959 580 rc = max_page;
kaf24@7959 581 break;
kaf24@7959 582
kaf24@7959 583 case XENMEM_current_reservation:
kaf24@7959 584 case XENMEM_maximum_reservation:
kaf24@9068 585 if ( copy_from_guest(&domid, arg, 1) )
kaf24@6486 586 return -EFAULT;
kaf24@7959 587
kaf24@9068 588 if ( likely(domid == DOMID_SELF) )
kaf24@7959 589 d = current->domain;
kaf24@7959 590 else if ( !IS_PRIV(current->domain) )
kaf24@7959 591 return -EPERM;
kaf24@7959 592 else if ( (d = find_domain_by_id(domid)) == NULL )
kaf24@7959 593 return -ESRCH;
kaf24@7959 594
kaf24@7959 595 rc = (op == XENMEM_current_reservation) ? d->tot_pages : d->max_pages;
kaf24@7959 596
kaf24@7959 597 if ( unlikely(domid != DOMID_SELF) )
kaf24@7959 598 put_domain(d);
kaf24@7959 599
kaf24@6486 600 break;
kaf24@6486 601
kaf24@8871 602 case XENMEM_translate_gpfn_list:
ack@13295 603 progress = cmd >> MEMOP_EXTENT_SHIFT;
kaf24@9068 604 rc = translate_gpfn_list(
kaf24@9068 605 guest_handle_cast(arg, xen_translate_gpfn_list_t),
kaf24@9068 606 &progress);
kaf24@8871 607 if ( rc == -EAGAIN )
kaf24@9068 608 return hypercall_create_continuation(
kaf24@9068 609 __HYPERVISOR_memory_op, "lh",
ack@13295 610 op | (progress << MEMOP_EXTENT_SHIFT), arg);
kaf24@8871 611 break;
kaf24@8871 612
kaf24@6486 613 default:
kaf24@8059 614 rc = arch_memory_op(op, arg);
kaf24@6486 615 break;
kaf24@6486 616 }
kaf24@6486 617
kaf24@6486 618 return rc;
kaf24@6486 619 }
kaf24@6486 620
kaf24@6486 621 /*
kaf24@6486 622 * Local variables:
kaf24@6486 623 * mode: C
kaf24@6486 624 * c-set-style: "BSD"
kaf24@6486 625 * c-basic-offset: 4
kaf24@6486 626 * tab-width: 4
kaf24@6486 627 * indent-tabs-mode: nil
kaf24@6486 628 * End:
kaf24@6486 629 */