ia64/xen-unstable

annotate xen/common/memory.c @ 11219:03fd2accb4d9

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