ia64/xen-unstable

annotate xen/common/memory.c @ 17986:4bdc3de246c3

numa: Extend MEMOP_ allocation functions to take a node argument.

The address_bits field will be limited to 8 bits and is now embedded
in the mem_flags member, which additionally contains the node number
(limited to 8 bit).

Signed-off-by: Andre Przywara <andre.przywara@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Sat Jul 05 14:43:37 2008 +0100 (2008-07-05)
parents 6e92603ed9f2
children 5eaec8296a22
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>
keir@17385 24 #include <xen/numa.h>
kaf24@6486 25 #include <public/memory.h>
kfraser@15815 26 #include <xsm/xsm.h>
kaf24@6486 27
kfraser@12374 28 struct memop_args {
kfraser@12374 29 /* INPUT */
kfraser@12374 30 struct domain *domain; /* Domain to be affected. */
kfraser@12374 31 XEN_GUEST_HANDLE(xen_pfn_t) extent_list; /* List of extent base addrs. */
kfraser@12374 32 unsigned int nr_extents; /* Number of extents to allocate or free. */
kfraser@12374 33 unsigned int extent_order; /* Size of each extent. */
kfraser@12374 34 unsigned int memflags; /* Allocation flags. */
kfraser@12374 35
kfraser@12374 36 /* INPUT/OUTPUT */
kfraser@12374 37 unsigned int nr_done; /* Number of extents processed so far. */
kfraser@12374 38 int preempted; /* Was the hypercall preempted? */
kfraser@12374 39 };
kfraser@12374 40
kfraser@12374 41 static void increase_reservation(struct memop_args *a)
kaf24@6486 42 {
kaf24@8726 43 struct page_info *page;
kaf24@10314 44 unsigned long i;
kaf24@10314 45 xen_pfn_t mfn;
kfraser@12374 46 struct domain *d = a->domain;
kaf24@6486 47
kfraser@12374 48 if ( !guest_handle_is_null(a->extent_list) &&
keir@17860 49 !guest_handle_subrange_okay(a->extent_list, a->nr_done,
keir@17860 50 a->nr_extents-1) )
kfraser@12374 51 return;
kaf24@6486 52
kfraser@12374 53 if ( (a->extent_order != 0) &&
kaf24@8468 54 !multipage_allocation_permitted(current->domain) )
kfraser@12374 55 return;
kaf24@6486 56
kfraser@12374 57 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@6486 58 {
kaf24@6486 59 if ( hypercall_preempt_check() )
kaf24@6607 60 {
kfraser@12374 61 a->preempted = 1;
kfraser@12374 62 goto out;
kaf24@6607 63 }
kaf24@6486 64
keir@17986 65 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
kfraser@12374 66 if ( unlikely(page == NULL) )
kaf24@6486 67 {
kaf24@12038 68 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
kfraser@10418 69 "id=%d memflags=%x (%ld of %d)\n",
kfraser@12374 70 a->extent_order, d->domain_id, a->memflags,
kfraser@12374 71 i, a->nr_extents);
kfraser@12374 72 goto out;
kaf24@6486 73 }
kaf24@6486 74
kaf24@6486 75 /* Inform the domain of the new page's machine address. */
kfraser@12374 76 if ( !guest_handle_is_null(a->extent_list) )
kaf24@8859 77 {
kaf24@8859 78 mfn = page_to_mfn(page);
kfraser@12374 79 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
kfraser@12374 80 goto out;
kaf24@8859 81 }
kaf24@6486 82 }
kaf24@6486 83
kfraser@12374 84 out:
kfraser@12374 85 a->nr_done = i;
kaf24@6486 86 }
sos22@8688 87
kfraser@12374 88 static void populate_physmap(struct memop_args *a)
kaf24@8673 89 {
kaf24@8726 90 struct page_info *page;
kaf24@10314 91 unsigned long i, j;
kfraser@12374 92 xen_pfn_t gpfn, mfn;
kfraser@12374 93 struct domain *d = a->domain;
kaf24@8673 94
keir@17860 95 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
keir@17860 96 a->nr_extents-1) )
kfraser@12374 97 return;
kaf24@8673 98
kfraser@12374 99 if ( (a->extent_order != 0) &&
kaf24@8673 100 !multipage_allocation_permitted(current->domain) )
kfraser@12374 101 return;
kaf24@8673 102
kfraser@12374 103 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@8673 104 {
kaf24@8673 105 if ( hypercall_preempt_check() )
kaf24@8673 106 {
kfraser@12374 107 a->preempted = 1;
sos22@8688 108 goto out;
kaf24@8673 109 }
kaf24@8673 110
kfraser@12374 111 if ( unlikely(__copy_from_guest_offset(&gpfn, a->extent_list, i, 1)) )
kaf24@8859 112 goto out;
kaf24@8859 113
keir@17986 114 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
kfraser@12374 115 if ( unlikely(page == NULL) )
kaf24@8673 116 {
kaf24@12038 117 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
kfraser@12374 118 "id=%d memflags=%x (%ld of %d)\n",
kfraser@12374 119 a->extent_order, d->domain_id, a->memflags,
kfraser@12374 120 i, a->nr_extents);
sos22@8688 121 goto out;
kaf24@8673 122 }
kaf24@8673 123
kaf24@8726 124 mfn = page_to_mfn(page);
keir@17727 125 guest_physmap_add_page(d, gpfn, mfn, a->extent_order);
kaf24@8673 126
keir@17727 127 if ( !paging_mode_translate(d) )
kaf24@8694 128 {
kfraser@12374 129 for ( j = 0; j < (1 << a->extent_order); j++ )
kaf24@8736 130 set_gpfn_from_mfn(mfn + j, gpfn + j);
kaf24@8673 131
sos22@8688 132 /* Inform the domain of the new page's machine address. */
kfraser@12374 133 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
sos22@8688 134 goto out;
sos22@8688 135 }
kaf24@8673 136 }
kaf24@8673 137
sos22@8688 138 out:
kfraser@12374 139 a->nr_done = i;
kaf24@8673 140 }
cl349@9211 141
kfraser@12374 142 int guest_remove_page(struct domain *d, unsigned long gmfn)
cl349@9211 143 {
cl349@9211 144 struct page_info *page;
cl349@9211 145 unsigned long mfn;
cl349@9211 146
cl349@9211 147 mfn = gmfn_to_mfn(d, gmfn);
cl349@9211 148 if ( unlikely(!mfn_valid(mfn)) )
cl349@9211 149 {
kaf24@12038 150 gdprintk(XENLOG_INFO, "Domain %u page number %lx invalid\n",
tdeegan@11172 151 d->domain_id, gmfn);
cl349@9211 152 return 0;
cl349@9211 153 }
cl349@9211 154
cl349@9211 155 page = mfn_to_page(mfn);
cl349@9211 156 if ( unlikely(!get_page(page, d)) )
cl349@9211 157 {
kaf24@12038 158 gdprintk(XENLOG_INFO, "Bad page free for domain %u\n", d->domain_id);
cl349@9211 159 return 0;
cl349@9211 160 }
cl349@9211 161
cl349@9211 162 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
cl349@9211 163 put_page_and_type(page);
cl349@9211 164
cl349@9211 165 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
cl349@9211 166 put_page(page);
cl349@9211 167
keir@17664 168 guest_physmap_remove_page(d, gmfn, mfn, 0);
cl349@9211 169
cl349@9211 170 put_page(page);
cl349@9211 171
cl349@9211 172 return 1;
cl349@9211 173 }
cl349@9211 174
kfraser@12374 175 static void decrease_reservation(struct memop_args *a)
kaf24@6486 176 {
kaf24@10314 177 unsigned long i, j;
kaf24@10314 178 xen_pfn_t gmfn;
kaf24@6486 179
keir@17860 180 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
keir@17860 181 a->nr_extents-1) )
kfraser@12374 182 return;
kaf24@6486 183
kfraser@12374 184 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@6486 185 {
kaf24@6486 186 if ( hypercall_preempt_check() )
kaf24@6607 187 {
kfraser@12374 188 a->preempted = 1;
kfraser@12374 189 goto out;
kaf24@6607 190 }
kaf24@6486 191
kfraser@12374 192 if ( unlikely(__copy_from_guest_offset(&gmfn, a->extent_list, i, 1)) )
kfraser@12374 193 goto out;
kaf24@6486 194
kfraser@12374 195 for ( j = 0; j < (1 << a->extent_order); j++ )
kfraser@12374 196 if ( !guest_remove_page(a->domain, gmfn + j) )
kfraser@12374 197 goto out;
kaf24@6486 198 }
kaf24@6486 199
kfraser@12374 200 out:
kfraser@12374 201 a->nr_done = i;
kaf24@6486 202 }
kaf24@6486 203
kfraser@12374 204 static long translate_gpfn_list(
kaf24@9873 205 XEN_GUEST_HANDLE(xen_translate_gpfn_list_t) uop, unsigned long *progress)
kaf24@8871 206 {
kaf24@8871 207 struct xen_translate_gpfn_list op;
kaf24@10314 208 unsigned long i;
kaf24@10314 209 xen_pfn_t gpfn;
kaf24@10314 210 xen_pfn_t mfn;
kaf24@8871 211 struct domain *d;
kfraser@15815 212 int rc;
kaf24@6486 213
kaf24@9068 214 if ( copy_from_guest(&op, uop, 1) )
kaf24@8871 215 return -EFAULT;
kaf24@8871 216
kaf24@8871 217 /* Is size too large for us to encode a continuation? */
ack@13295 218 if ( op.nr_gpfns > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kaf24@8871 219 return -EINVAL;
kaf24@8871 220
keir@17860 221 if ( !guest_handle_subrange_okay(op.gpfn_list, *progress, op.nr_gpfns-1) ||
keir@17860 222 !guest_handle_subrange_okay(op.mfn_list, *progress, op.nr_gpfns-1) )
kaf24@8871 223 return -EFAULT;
kaf24@8871 224
kaf24@8871 225 if ( op.domid == DOMID_SELF )
keir@17349 226 {
keir@17349 227 d = rcu_lock_current_domain();
keir@17349 228 }
keir@17349 229 else
keir@17349 230 {
keir@17349 231 if ( (d = rcu_lock_domain_by_id(op.domid)) == NULL )
keir@16856 232 return -ESRCH;
keir@17349 233 if ( !IS_PRIV_FOR(current->domain, d) )
keir@17349 234 {
keir@16856 235 rcu_unlock_domain(d);
keir@16856 236 return -EPERM;
keir@16856 237 }
keir@16856 238 }
kaf24@8871 239
kaf24@8871 240
Tim@15635 241 if ( !paging_mode_translate(d) )
kaf24@8871 242 {
kfraser@14192 243 rcu_unlock_domain(d);
kaf24@8871 244 return -EINVAL;
kaf24@8871 245 }
kaf24@8871 246
kaf24@8871 247 for ( i = *progress; i < op.nr_gpfns; i++ )
kaf24@8871 248 {
kaf24@8871 249 if ( hypercall_preempt_check() )
kaf24@8871 250 {
kfraser@14192 251 rcu_unlock_domain(d);
kaf24@8871 252 *progress = i;
kaf24@8871 253 return -EAGAIN;
kaf24@8871 254 }
kaf24@8871 255
kaf24@9068 256 if ( unlikely(__copy_from_guest_offset(&gpfn, op.gpfn_list, i, 1)) )
kaf24@8871 257 {
kfraser@14192 258 rcu_unlock_domain(d);
kaf24@8871 259 return -EFAULT;
kaf24@8871 260 }
kaf24@8871 261
kaf24@8871 262 mfn = gmfn_to_mfn(d, gpfn);
kaf24@8871 263
kfraser@15815 264 rc = xsm_translate_gpfn_list(current->domain, mfn);
kfraser@15815 265 if ( rc )
kfraser@15815 266 {
kfraser@15815 267 rcu_unlock_domain(d);
kfraser@15815 268 return rc;
kfraser@15815 269 }
kfraser@15815 270
kaf24@9068 271 if ( unlikely(__copy_to_guest_offset(op.mfn_list, i, &mfn, 1)) )
kaf24@8871 272 {
kfraser@14192 273 rcu_unlock_domain(d);
kaf24@8871 274 return -EFAULT;
kaf24@8871 275 }
kaf24@8871 276 }
kaf24@8871 277
kfraser@14192 278 rcu_unlock_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;
keir@17385 290 unsigned int memflags = 0;
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.in.extent_order <= exch.out.extent_order )
kfraser@10418 322 {
kfraser@10418 323 in_chunk_order = exch.out.extent_order - exch.in.extent_order;
kfraser@10418 324 out_chunk_order = 0;
kfraser@10418 325 }
kfraser@10418 326 else
kfraser@10418 327 {
kfraser@10418 328 in_chunk_order = 0;
kfraser@10418 329 out_chunk_order = exch.in.extent_order - exch.out.extent_order;
kfraser@10418 330 }
kfraser@10418 331
kfraser@10418 332 /*
kfraser@10418 333 * Only support exchange on calling domain right now. Otherwise there are
kfraser@14642 334 * tricky corner cases to consider (e.g., dying domain).
kfraser@10418 335 */
kfraser@10418 336 if ( unlikely(exch.in.domid != DOMID_SELF) )
kfraser@10418 337 {
kfraser@10418 338 rc = IS_PRIV(current->domain) ? -EINVAL : -EPERM;
kfraser@10418 339 goto fail_early;
kfraser@10418 340 }
kfraser@10418 341 d = current->domain;
kfraser@10418 342
keir@16548 343 memflags |= MEMF_bits(domain_clamp_alloc_bitsize(
keir@17986 344 d,
keir@17986 345 XENMEMF_get_address_bits(exch.out.mem_flags) ? :
keir@17986 346 (BITS_PER_LONG+PAGE_SHIFT)));
keir@17986 347 memflags |= MEMF_node(XENMEMF_get_node(exch.out.mem_flags));
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 {
keir@17385 396 page = alloc_domheap_pages(NULL, exch.out.extent_order, memflags);
kfraser@10418 397 if ( unlikely(page == NULL) )
kfraser@10418 398 {
kfraser@10418 399 rc = -ENOMEM;
kfraser@10418 400 goto fail;
kfraser@10418 401 }
kfraser@10418 402
kfraser@10418 403 list_add(&page->list, &out_chunk_list);
kfraser@10418 404 }
kfraser@10418 405
kfraser@10418 406 /*
kfraser@10418 407 * Success! Beyond this point we cannot fail for this chunk.
kfraser@10418 408 */
kfraser@10418 409
kfraser@10418 410 /* Destroy final reference to each input page. */
kfraser@10418 411 while ( !list_empty(&in_chunk_list) )
kfraser@10418 412 {
kfraser@10418 413 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 414 list_del(&page->list);
kfraser@10418 415 if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
kfraser@10418 416 BUG();
kfraser@10418 417 mfn = page_to_mfn(page);
keir@17664 418 guest_physmap_remove_page(d, mfn_to_gmfn(d, mfn), mfn, 0);
kfraser@10418 419 put_page(page);
kfraser@10418 420 }
kfraser@10418 421
kfraser@10418 422 /* Assign each output page to the domain. */
kfraser@10418 423 j = 0;
kfraser@10418 424 while ( !list_empty(&out_chunk_list) )
kfraser@10418 425 {
kfraser@10418 426 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 427 list_del(&page->list);
kfraser@10418 428 if ( assign_pages(d, page, exch.out.extent_order,
kfraser@10418 429 MEMF_no_refcount) )
kfraser@10418 430 BUG();
kfraser@10418 431
kfraser@10418 432 /* Note that we ignore errors accessing the output extent list. */
kfraser@10418 433 (void)__copy_from_guest_offset(
kfraser@10418 434 &gpfn, exch.out.extent_start, (i<<out_chunk_order)+j, 1);
kfraser@10418 435
kfraser@10418 436 mfn = page_to_mfn(page);
keir@17727 437 guest_physmap_add_page(d, gpfn, mfn, exch.out.extent_order);
keir@17727 438
keir@17727 439 if ( !paging_mode_translate(d) )
kfraser@10418 440 {
kfraser@10418 441 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
kfraser@10418 442 set_gpfn_from_mfn(mfn + k, gpfn + k);
kfraser@10418 443 (void)__copy_to_guest_offset(
kfraser@10418 444 exch.out.extent_start, (i<<out_chunk_order)+j, &mfn, 1);
kfraser@10418 445 }
kfraser@10418 446
kfraser@10418 447 j++;
kfraser@10418 448 }
kfraser@10418 449 BUG_ON(j != (1UL << out_chunk_order));
kfraser@10418 450 }
kfraser@10418 451
kfraser@12374 452 exch.nr_exchanged = exch.in.nr_extents;
kfraser@10418 453 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 454 rc = -EFAULT;
kfraser@10418 455 return rc;
kfraser@10418 456
kfraser@10418 457 /*
kfraser@10418 458 * Failed a chunk! Free any partial chunk work. Tell caller how many
kfraser@10418 459 * chunks succeeded.
kfraser@10418 460 */
kfraser@10418 461 fail:
kfraser@10418 462 /* Reassign any input pages we managed to steal. */
kfraser@10418 463 while ( !list_empty(&in_chunk_list) )
kfraser@10418 464 {
kfraser@10418 465 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 466 list_del(&page->list);
kfraser@10418 467 if ( assign_pages(d, page, 0, MEMF_no_refcount) )
kfraser@10418 468 BUG();
kfraser@10418 469 }
kfraser@10418 470
kfraser@10418 471 /* Free any output pages we managed to allocate. */
kfraser@10418 472 while ( !list_empty(&out_chunk_list) )
kfraser@10418 473 {
kfraser@10418 474 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 475 list_del(&page->list);
kfraser@10418 476 free_domheap_pages(page, exch.out.extent_order);
kfraser@10418 477 }
kfraser@10418 478
kfraser@12374 479 exch.nr_exchanged = i << in_chunk_order;
kfraser@10418 480
kfraser@10418 481 fail_early:
kfraser@10418 482 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 483 rc = -EFAULT;
kfraser@10418 484 return rc;
kfraser@10418 485 }
kfraser@10418 486
kaf24@9873 487 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE(void) arg)
kaf24@6486 488 {
kaf24@6486 489 struct domain *d;
kfraser@12374 490 int rc, op;
keir@17986 491 unsigned int address_bits;
kaf24@8871 492 unsigned long start_extent, progress;
kaf24@6486 493 struct xen_memory_reservation reservation;
kfraser@12374 494 struct memop_args args;
kaf24@7959 495 domid_t domid;
kaf24@6486 496
ack@13295 497 op = cmd & MEMOP_CMD_MASK;
kaf24@6486 498
kaf24@6486 499 switch ( op )
kaf24@6486 500 {
kaf24@6486 501 case XENMEM_increase_reservation:
kaf24@6486 502 case XENMEM_decrease_reservation:
kaf24@8673 503 case XENMEM_populate_physmap:
ack@13295 504 start_extent = cmd >> MEMOP_EXTENT_SHIFT;
kfraser@10418 505
kaf24@9068 506 if ( copy_from_guest(&reservation, arg, 1) )
kfraser@10418 507 return start_extent;
kaf24@6486 508
kaf24@8871 509 /* Is size too large for us to encode a continuation? */
ack@13295 510 if ( reservation.nr_extents > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kfraser@10418 511 return start_extent;
kaf24@8871 512
kaf24@6486 513 if ( unlikely(start_extent > reservation.nr_extents) )
kfraser@10418 514 return start_extent;
kaf24@9068 515
kfraser@12374 516 args.extent_list = reservation.extent_start;
kfraser@12374 517 args.nr_extents = reservation.nr_extents;
kfraser@12374 518 args.extent_order = reservation.extent_order;
kfraser@12374 519 args.nr_done = start_extent;
kfraser@12374 520 args.preempted = 0;
kfraser@12374 521 args.memflags = 0;
kaf24@6486 522
keir@17986 523 address_bits = XENMEMF_get_address_bits(reservation.mem_flags);
keir@17986 524 if ( (address_bits != 0) &&
keir@17986 525 (address_bits < (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kaf24@6486 526 {
keir@17986 527 if ( address_bits <= PAGE_SHIFT )
kfraser@10418 528 return start_extent;
keir@17986 529 args.memflags = MEMF_bits(address_bits);
kaf24@6486 530 }
kaf24@6486 531
keir@17986 532 args.memflags |= MEMF_node(XENMEMF_get_node(reservation.mem_flags));
keir@17986 533
kaf24@6486 534 if ( likely(reservation.domid == DOMID_SELF) )
keir@17349 535 {
keir@17349 536 d = rcu_lock_current_domain();
keir@17349 537 }
keir@17349 538 else
keir@17349 539 {
keir@17349 540 if ( (d = rcu_lock_domain_by_id(reservation.domid)) == NULL )
keir@16856 541 return start_extent;
keir@17349 542 if ( !IS_PRIV_FOR(current->domain, d) )
keir@17349 543 {
keir@16856 544 rcu_unlock_domain(d);
keir@16856 545 return start_extent;
keir@16856 546 }
keir@16856 547 }
kfraser@12374 548 args.domain = d;
kaf24@6486 549
kfraser@15815 550 rc = xsm_memory_adjust_reservation(current->domain, d);
kfraser@15815 551 if ( rc )
kfraser@15815 552 {
keir@17349 553 rcu_unlock_domain(d);
kfraser@15815 554 return rc;
kfraser@15815 555 }
kfraser@15815 556
kaf24@8673 557 switch ( op )
kaf24@8673 558 {
kaf24@8673 559 case XENMEM_increase_reservation:
kfraser@12374 560 increase_reservation(&args);
kaf24@8673 561 break;
kaf24@8673 562 case XENMEM_decrease_reservation:
kfraser@12374 563 decrease_reservation(&args);
kaf24@8673 564 break;
kfraser@12374 565 default: /* XENMEM_populate_physmap */
kfraser@12374 566 populate_physmap(&args);
kaf24@8673 567 break;
kaf24@8673 568 }
kaf24@6486 569
keir@17349 570 rcu_unlock_domain(d);
kaf24@6486 571
kfraser@12374 572 rc = args.nr_done;
kaf24@6486 573
kfraser@12374 574 if ( args.preempted )
kaf24@9068 575 return hypercall_create_continuation(
kaf24@9068 576 __HYPERVISOR_memory_op, "lh",
ack@13295 577 op | (rc << MEMOP_EXTENT_SHIFT), arg);
kaf24@6607 578
kaf24@6486 579 break;
kaf24@6486 580
kfraser@10418 581 case XENMEM_exchange:
kfraser@10418 582 rc = memory_exchange(guest_handle_cast(arg, xen_memory_exchange_t));
kfraser@10418 583 break;
kfraser@10418 584
kaf24@6486 585 case XENMEM_maximum_ram_page:
kaf24@7959 586 rc = max_page;
kaf24@7959 587 break;
kaf24@7959 588
kaf24@7959 589 case XENMEM_current_reservation:
kaf24@7959 590 case XENMEM_maximum_reservation:
kfraser@14471 591 case XENMEM_maximum_gpfn:
kaf24@9068 592 if ( copy_from_guest(&domid, arg, 1) )
kaf24@6486 593 return -EFAULT;
kaf24@7959 594
kaf24@9068 595 if ( likely(domid == DOMID_SELF) )
keir@17349 596 {
keir@17349 597 d = rcu_lock_current_domain();
keir@17349 598 }
keir@17349 599 else
keir@17349 600 {
keir@17349 601 if ( (d = rcu_lock_domain_by_id(domid)) == NULL )
keir@16856 602 return -ESRCH;
keir@17349 603 if ( !IS_PRIV_FOR(current->domain, d) )
keir@17349 604 {
keir@16856 605 rcu_unlock_domain(d);
keir@16856 606 return -EPERM;
keir@16856 607 }
keir@16856 608 }
kaf24@7959 609
kfraser@15815 610 rc = xsm_memory_stat_reservation(current->domain, d);
kfraser@15815 611 if ( rc )
kfraser@15815 612 {
keir@17349 613 rcu_unlock_domain(d);
kfraser@15815 614 return rc;
kfraser@15815 615 }
kfraser@15815 616
kfraser@14471 617 switch ( op )
kfraser@14471 618 {
kfraser@14471 619 case XENMEM_current_reservation:
kfraser@14471 620 rc = d->tot_pages;
kfraser@14471 621 break;
kfraser@14471 622 case XENMEM_maximum_reservation:
kfraser@14471 623 rc = d->max_pages;
kfraser@14471 624 break;
kfraser@14471 625 default:
kfraser@14471 626 ASSERT(op == XENMEM_maximum_gpfn);
kfraser@14471 627 rc = domain_get_maximum_gpfn(d);
kfraser@14471 628 break;
kfraser@14471 629 }
kaf24@7959 630
keir@17349 631 rcu_unlock_domain(d);
kaf24@7959 632
kaf24@6486 633 break;
kaf24@6486 634
kaf24@8871 635 case XENMEM_translate_gpfn_list:
ack@13295 636 progress = cmd >> MEMOP_EXTENT_SHIFT;
kaf24@9068 637 rc = translate_gpfn_list(
kaf24@9068 638 guest_handle_cast(arg, xen_translate_gpfn_list_t),
kaf24@9068 639 &progress);
kaf24@8871 640 if ( rc == -EAGAIN )
kaf24@9068 641 return hypercall_create_continuation(
kaf24@9068 642 __HYPERVISOR_memory_op, "lh",
ack@13295 643 op | (progress << MEMOP_EXTENT_SHIFT), arg);
kaf24@8871 644 break;
kaf24@8871 645
kaf24@6486 646 default:
kaf24@8059 647 rc = arch_memory_op(op, arg);
kaf24@6486 648 break;
kaf24@6486 649 }
kaf24@6486 650
kaf24@6486 651 return rc;
kaf24@6486 652 }
kaf24@6486 653
kaf24@6486 654 /*
kaf24@6486 655 * Local variables:
kaf24@6486 656 * mode: C
kaf24@6486 657 * c-set-style: "BSD"
kaf24@6486 658 * c-basic-offset: 4
kaf24@6486 659 * tab-width: 4
kaf24@6486 660 * indent-tabs-mode: nil
kaf24@6486 661 * End:
kaf24@6486 662 */