ia64/xen-unstable

annotate xen/common/memory.c @ 11172:0f917d63e960

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