ia64/xen-unstable

annotate xen/common/memory.c @ 16548:cd5e1e76d0bc

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