ia64/xen-unstable

annotate xen/common/memory.c @ 15815:96f64f4c42f0

Xen Security Modules: XSM
Signed-off-by: George Coker <gscoker@alpha.ncsc.mil>
author kfraser@localhost.localdomain
date Fri Aug 31 11:21:35 2007 +0100 (2007-08-31)
parents 3c28bc13a3f8
children 2717128cbdd1
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++ )
kaf24@8736 134 guest_physmap_add_page(d, gpfn + j, mfn + j);
sos22@8688 135 }
kaf24@8694 136 else
kaf24@8694 137 {
kfraser@12374 138 for ( j = 0; j < (1 << a->extent_order); j++ )
kaf24@8736 139 set_gpfn_from_mfn(mfn + j, gpfn + j);
kaf24@8673 140
sos22@8688 141 /* Inform the domain of the new page's machine address. */
kfraser@12374 142 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
sos22@8688 143 goto out;
sos22@8688 144 }
kaf24@8673 145 }
kaf24@8673 146
sos22@8688 147 out:
kfraser@12374 148 a->nr_done = i;
kaf24@8673 149 }
cl349@9211 150
kfraser@12374 151 int guest_remove_page(struct domain *d, unsigned long gmfn)
cl349@9211 152 {
cl349@9211 153 struct page_info *page;
cl349@9211 154 unsigned long mfn;
cl349@9211 155
cl349@9211 156 mfn = gmfn_to_mfn(d, gmfn);
cl349@9211 157 if ( unlikely(!mfn_valid(mfn)) )
cl349@9211 158 {
kaf24@12038 159 gdprintk(XENLOG_INFO, "Domain %u page number %lx invalid\n",
tdeegan@11172 160 d->domain_id, gmfn);
cl349@9211 161 return 0;
cl349@9211 162 }
cl349@9211 163
cl349@9211 164 page = mfn_to_page(mfn);
cl349@9211 165 if ( unlikely(!get_page(page, d)) )
cl349@9211 166 {
kaf24@12038 167 gdprintk(XENLOG_INFO, "Bad page free for domain %u\n", d->domain_id);
cl349@9211 168 return 0;
cl349@9211 169 }
cl349@9211 170
cl349@9211 171 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
cl349@9211 172 put_page_and_type(page);
cl349@9211 173
cl349@9211 174 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
cl349@9211 175 put_page(page);
cl349@9211 176
kfraser@11212 177 guest_physmap_remove_page(d, gmfn, mfn);
cl349@9211 178
cl349@9211 179 put_page(page);
cl349@9211 180
cl349@9211 181 return 1;
cl349@9211 182 }
cl349@9211 183
kfraser@12374 184 static void decrease_reservation(struct memop_args *a)
kaf24@6486 185 {
kaf24@10314 186 unsigned long i, j;
kaf24@10314 187 xen_pfn_t gmfn;
kaf24@6486 188
kfraser@12374 189 if ( !guest_handle_okay(a->extent_list, a->nr_extents) )
kfraser@12374 190 return;
kaf24@6486 191
kfraser@12374 192 for ( i = a->nr_done; i < a->nr_extents; i++ )
kaf24@6486 193 {
kaf24@6486 194 if ( hypercall_preempt_check() )
kaf24@6607 195 {
kfraser@12374 196 a->preempted = 1;
kfraser@12374 197 goto out;
kaf24@6607 198 }
kaf24@6486 199
kfraser@12374 200 if ( unlikely(__copy_from_guest_offset(&gmfn, a->extent_list, i, 1)) )
kfraser@12374 201 goto out;
kaf24@6486 202
kfraser@12374 203 for ( j = 0; j < (1 << a->extent_order); j++ )
kfraser@12374 204 if ( !guest_remove_page(a->domain, gmfn + j) )
kfraser@12374 205 goto out;
kaf24@6486 206 }
kaf24@6486 207
kfraser@12374 208 out:
kfraser@12374 209 a->nr_done = i;
kaf24@6486 210 }
kaf24@6486 211
kfraser@12374 212 static long translate_gpfn_list(
kaf24@9873 213 XEN_GUEST_HANDLE(xen_translate_gpfn_list_t) uop, unsigned long *progress)
kaf24@8871 214 {
kaf24@8871 215 struct xen_translate_gpfn_list op;
kaf24@10314 216 unsigned long i;
kaf24@10314 217 xen_pfn_t gpfn;
kaf24@10314 218 xen_pfn_t mfn;
kaf24@8871 219 struct domain *d;
kfraser@15815 220 int rc;
kaf24@6486 221
kaf24@9068 222 if ( copy_from_guest(&op, uop, 1) )
kaf24@8871 223 return -EFAULT;
kaf24@8871 224
kaf24@8871 225 /* Is size too large for us to encode a continuation? */
ack@13295 226 if ( op.nr_gpfns > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kaf24@8871 227 return -EINVAL;
kaf24@8871 228
kaf24@9068 229 if ( !guest_handle_okay(op.gpfn_list, op.nr_gpfns) ||
kaf24@9068 230 !guest_handle_okay(op.mfn_list, op.nr_gpfns) )
kaf24@8871 231 return -EFAULT;
kaf24@8871 232
kaf24@8871 233 if ( op.domid == DOMID_SELF )
kaf24@8871 234 op.domid = current->domain->domain_id;
kaf24@8871 235 else if ( !IS_PRIV(current->domain) )
kaf24@8871 236 return -EPERM;
kaf24@8871 237
kfraser@14192 238 if ( (d = rcu_lock_domain_by_id(op.domid)) == NULL )
kaf24@8871 239 return -ESRCH;
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;
kfraser@11973 290 unsigned int memflags = 0, cpu;
kfraser@10418 291 long rc = 0;
kfraser@10418 292 struct domain *d;
kfraser@10418 293 struct page_info *page;
kfraser@10418 294
kfraser@10418 295 if ( copy_from_guest(&exch, arg, 1) )
kfraser@10418 296 return -EFAULT;
kfraser@10418 297
kfraser@10418 298 /* Various sanity checks. */
kfraser@10418 299 if ( (exch.nr_exchanged > exch.in.nr_extents) ||
kfraser@10418 300 /* Input and output domain identifiers match? */
kfraser@10418 301 (exch.in.domid != exch.out.domid) ||
kfraser@10418 302 /* Sizes of input and output lists do not overflow a long? */
kfraser@10418 303 ((~0UL >> exch.in.extent_order) < exch.in.nr_extents) ||
kfraser@10418 304 ((~0UL >> exch.out.extent_order) < exch.out.nr_extents) ||
kfraser@10418 305 /* Sizes of input and output lists match? */
kfraser@10418 306 ((exch.in.nr_extents << exch.in.extent_order) !=
kfraser@10418 307 (exch.out.nr_extents << exch.out.extent_order)) )
kfraser@10418 308 {
kfraser@10418 309 rc = -EINVAL;
kfraser@10418 310 goto fail_early;
kfraser@10418 311 }
kfraser@10418 312
kfraser@10418 313 /* Only privileged guests can allocate multi-page contiguous extents. */
kfraser@10418 314 if ( ((exch.in.extent_order != 0) || (exch.out.extent_order != 0)) &&
kfraser@10418 315 !multipage_allocation_permitted(current->domain) )
kfraser@10418 316 {
kfraser@10418 317 rc = -EPERM;
kfraser@10418 318 goto fail_early;
kfraser@10418 319 }
kfraser@10418 320
kfraser@10418 321 if ( (exch.out.address_bits != 0) &&
kfraser@10418 322 (exch.out.address_bits <
kfraser@10418 323 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kfraser@10418 324 {
kfraser@14103 325 if ( exch.out.address_bits <= PAGE_SHIFT )
kfraser@10418 326 {
kfraser@10418 327 rc = -ENOMEM;
kfraser@10418 328 goto fail_early;
kfraser@10418 329 }
kfraser@14103 330 memflags = MEMF_bits(exch.out.address_bits);
kfraser@10418 331 }
kfraser@10418 332
kfraser@10418 333 if ( exch.in.extent_order <= exch.out.extent_order )
kfraser@10418 334 {
kfraser@10418 335 in_chunk_order = exch.out.extent_order - exch.in.extent_order;
kfraser@10418 336 out_chunk_order = 0;
kfraser@10418 337 }
kfraser@10418 338 else
kfraser@10418 339 {
kfraser@10418 340 in_chunk_order = 0;
kfraser@10418 341 out_chunk_order = exch.in.extent_order - exch.out.extent_order;
kfraser@10418 342 }
kfraser@10418 343
kfraser@10418 344 /*
kfraser@10418 345 * Only support exchange on calling domain right now. Otherwise there are
kfraser@14642 346 * tricky corner cases to consider (e.g., dying domain).
kfraser@10418 347 */
kfraser@10418 348 if ( unlikely(exch.in.domid != DOMID_SELF) )
kfraser@10418 349 {
kfraser@10418 350 rc = IS_PRIV(current->domain) ? -EINVAL : -EPERM;
kfraser@10418 351 goto fail_early;
kfraser@10418 352 }
kfraser@10418 353 d = current->domain;
kfraser@10418 354
kfraser@12374 355 cpu = select_local_cpu(d);
kfraser@11973 356
kfraser@12374 357 for ( i = (exch.nr_exchanged >> in_chunk_order);
kfraser@12374 358 i < (exch.in.nr_extents >> in_chunk_order);
kfraser@12374 359 i++ )
kfraser@10418 360 {
kfraser@10418 361 if ( hypercall_preempt_check() )
kfraser@10418 362 {
kfraser@12374 363 exch.nr_exchanged = i << in_chunk_order;
kfraser@10418 364 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 365 return -EFAULT;
kfraser@10418 366 return hypercall_create_continuation(
kfraser@10418 367 __HYPERVISOR_memory_op, "lh", XENMEM_exchange, arg);
kfraser@10418 368 }
kfraser@10418 369
kfraser@10418 370 /* Steal a chunk's worth of input pages from the domain. */
kfraser@10418 371 for ( j = 0; j < (1UL << in_chunk_order); j++ )
kfraser@10418 372 {
kfraser@10418 373 if ( unlikely(__copy_from_guest_offset(
kfraser@10418 374 &gmfn, exch.in.extent_start, (i<<in_chunk_order)+j, 1)) )
kfraser@10418 375 {
kfraser@10418 376 rc = -EFAULT;
kfraser@10418 377 goto fail;
kfraser@10418 378 }
kfraser@10418 379
kfraser@10418 380 for ( k = 0; k < (1UL << exch.in.extent_order); k++ )
kfraser@10418 381 {
kfraser@10418 382 mfn = gmfn_to_mfn(d, gmfn + k);
kfraser@10418 383 if ( unlikely(!mfn_valid(mfn)) )
kfraser@10418 384 {
kfraser@10418 385 rc = -EINVAL;
kfraser@10418 386 goto fail;
kfraser@10418 387 }
kfraser@10418 388
kfraser@10418 389 page = mfn_to_page(mfn);
kfraser@10418 390
kfraser@10418 391 if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
kfraser@10418 392 {
kfraser@10418 393 rc = -EINVAL;
kfraser@10418 394 goto fail;
kfraser@10418 395 }
kfraser@10418 396
kfraser@10418 397 list_add(&page->list, &in_chunk_list);
kfraser@10418 398 }
kfraser@10418 399 }
kfraser@10418 400
kfraser@10418 401 /* Allocate a chunk's worth of anonymous output pages. */
kfraser@10418 402 for ( j = 0; j < (1UL << out_chunk_order); j++ )
kfraser@10418 403 {
kfraser@12374 404 page = __alloc_domheap_pages(
kfraser@12374 405 NULL, cpu, exch.out.extent_order, memflags);
kfraser@10418 406 if ( unlikely(page == NULL) )
kfraser@10418 407 {
kfraser@10418 408 rc = -ENOMEM;
kfraser@10418 409 goto fail;
kfraser@10418 410 }
kfraser@10418 411
kfraser@10418 412 list_add(&page->list, &out_chunk_list);
kfraser@10418 413 }
kfraser@10418 414
kfraser@10418 415 /*
kfraser@10418 416 * Success! Beyond this point we cannot fail for this chunk.
kfraser@10418 417 */
kfraser@10418 418
kfraser@10418 419 /* Destroy final reference to each input page. */
kfraser@10418 420 while ( !list_empty(&in_chunk_list) )
kfraser@10418 421 {
kfraser@10418 422 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 423 list_del(&page->list);
kfraser@10418 424 if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
kfraser@10418 425 BUG();
kfraser@10418 426 mfn = page_to_mfn(page);
kfraser@10418 427 guest_physmap_remove_page(d, mfn_to_gmfn(d, mfn), mfn);
kfraser@10418 428 put_page(page);
kfraser@10418 429 }
kfraser@10418 430
kfraser@10418 431 /* Assign each output page to the domain. */
kfraser@10418 432 j = 0;
kfraser@10418 433 while ( !list_empty(&out_chunk_list) )
kfraser@10418 434 {
kfraser@10418 435 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 436 list_del(&page->list);
kfraser@10418 437 if ( assign_pages(d, page, exch.out.extent_order,
kfraser@10418 438 MEMF_no_refcount) )
kfraser@10418 439 BUG();
kfraser@10418 440
kfraser@10418 441 /* Note that we ignore errors accessing the output extent list. */
kfraser@10418 442 (void)__copy_from_guest_offset(
kfraser@10418 443 &gpfn, exch.out.extent_start, (i<<out_chunk_order)+j, 1);
kfraser@10418 444
kfraser@10418 445 mfn = page_to_mfn(page);
Tim@15635 446 if ( unlikely(paging_mode_translate(d)) )
kfraser@10418 447 {
kfraser@10418 448 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
kfraser@10418 449 guest_physmap_add_page(d, gpfn + k, mfn + k);
kfraser@10418 450 }
kfraser@10418 451 else
kfraser@10418 452 {
kfraser@10418 453 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
kfraser@10418 454 set_gpfn_from_mfn(mfn + k, gpfn + k);
kfraser@10418 455 (void)__copy_to_guest_offset(
kfraser@10418 456 exch.out.extent_start, (i<<out_chunk_order)+j, &mfn, 1);
kfraser@10418 457 }
kfraser@10418 458
kfraser@10418 459 j++;
kfraser@10418 460 }
kfraser@10418 461 BUG_ON(j != (1UL << out_chunk_order));
kfraser@10418 462 }
kfraser@10418 463
kfraser@12374 464 exch.nr_exchanged = exch.in.nr_extents;
kfraser@10418 465 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 466 rc = -EFAULT;
kfraser@10418 467 return rc;
kfraser@10418 468
kfraser@10418 469 /*
kfraser@10418 470 * Failed a chunk! Free any partial chunk work. Tell caller how many
kfraser@10418 471 * chunks succeeded.
kfraser@10418 472 */
kfraser@10418 473 fail:
kfraser@10418 474 /* Reassign any input pages we managed to steal. */
kfraser@10418 475 while ( !list_empty(&in_chunk_list) )
kfraser@10418 476 {
kfraser@10418 477 page = list_entry(in_chunk_list.next, struct page_info, list);
kfraser@10418 478 list_del(&page->list);
kfraser@10418 479 if ( assign_pages(d, page, 0, MEMF_no_refcount) )
kfraser@10418 480 BUG();
kfraser@10418 481 }
kfraser@10418 482
kfraser@10418 483 /* Free any output pages we managed to allocate. */
kfraser@10418 484 while ( !list_empty(&out_chunk_list) )
kfraser@10418 485 {
kfraser@10418 486 page = list_entry(out_chunk_list.next, struct page_info, list);
kfraser@10418 487 list_del(&page->list);
kfraser@10418 488 free_domheap_pages(page, exch.out.extent_order);
kfraser@10418 489 }
kfraser@10418 490
kfraser@12374 491 exch.nr_exchanged = i << in_chunk_order;
kfraser@10418 492
kfraser@10418 493 fail_early:
kfraser@10418 494 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
kfraser@10418 495 rc = -EFAULT;
kfraser@10418 496 return rc;
kfraser@10418 497 }
kfraser@10418 498
kaf24@9873 499 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE(void) arg)
kaf24@6486 500 {
kaf24@6486 501 struct domain *d;
kfraser@12374 502 int rc, op;
kaf24@8871 503 unsigned long start_extent, progress;
kaf24@6486 504 struct xen_memory_reservation reservation;
kfraser@12374 505 struct memop_args args;
kaf24@7959 506 domid_t domid;
kaf24@6486 507
ack@13295 508 op = cmd & MEMOP_CMD_MASK;
kaf24@6486 509
kaf24@6486 510 switch ( op )
kaf24@6486 511 {
kaf24@6486 512 case XENMEM_increase_reservation:
kaf24@6486 513 case XENMEM_decrease_reservation:
kaf24@8673 514 case XENMEM_populate_physmap:
ack@13295 515 start_extent = cmd >> MEMOP_EXTENT_SHIFT;
kfraser@10418 516
kaf24@9068 517 if ( copy_from_guest(&reservation, arg, 1) )
kfraser@10418 518 return start_extent;
kaf24@6486 519
kaf24@8871 520 /* Is size too large for us to encode a continuation? */
ack@13295 521 if ( reservation.nr_extents > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
kfraser@10418 522 return start_extent;
kaf24@8871 523
kaf24@6486 524 if ( unlikely(start_extent > reservation.nr_extents) )
kfraser@10418 525 return start_extent;
kaf24@9068 526
kfraser@12374 527 args.extent_list = reservation.extent_start;
kfraser@12374 528 args.nr_extents = reservation.nr_extents;
kfraser@12374 529 args.extent_order = reservation.extent_order;
kfraser@12374 530 args.nr_done = start_extent;
kfraser@12374 531 args.preempted = 0;
kfraser@12374 532 args.memflags = 0;
kaf24@6486 533
kaf24@6701 534 if ( (reservation.address_bits != 0) &&
kaf24@6702 535 (reservation.address_bits <
kaf24@6702 536 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kaf24@6486 537 {
kfraser@14103 538 if ( reservation.address_bits <= PAGE_SHIFT )
kfraser@10418 539 return start_extent;
kfraser@14103 540 args.memflags = MEMF_bits(reservation.address_bits);
kaf24@6486 541 }
kaf24@6486 542
kaf24@6486 543 if ( likely(reservation.domid == DOMID_SELF) )
kaf24@6486 544 d = current->domain;
kfraser@10418 545 else if ( !IS_PRIV(current->domain) ||
kfraser@14192 546 ((d = rcu_lock_domain_by_id(reservation.domid)) == NULL) )
kfraser@10418 547 return start_extent;
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 {
kfraser@15815 553 if ( reservation.domid != DOMID_SELF )
kfraser@15815 554 rcu_unlock_domain(d);
kfraser@15815 555 return rc;
kfraser@15815 556 }
kfraser@15815 557
kaf24@8673 558 switch ( op )
kaf24@8673 559 {
kaf24@8673 560 case XENMEM_increase_reservation:
kfraser@12374 561 increase_reservation(&args);
kaf24@8673 562 break;
kaf24@8673 563 case XENMEM_decrease_reservation:
kfraser@12374 564 decrease_reservation(&args);
kaf24@8673 565 break;
kfraser@12374 566 default: /* XENMEM_populate_physmap */
kfraser@12374 567 populate_physmap(&args);
kaf24@8673 568 break;
kaf24@8673 569 }
kaf24@6486 570
kaf24@6486 571 if ( unlikely(reservation.domid != DOMID_SELF) )
kfraser@14192 572 rcu_unlock_domain(d);
kaf24@6486 573
kfraser@12374 574 rc = args.nr_done;
kaf24@6486 575
kfraser@12374 576 if ( args.preempted )
kaf24@9068 577 return hypercall_create_continuation(
kaf24@9068 578 __HYPERVISOR_memory_op, "lh",
ack@13295 579 op | (rc << MEMOP_EXTENT_SHIFT), arg);
kaf24@6607 580
kaf24@6486 581 break;
kaf24@6486 582
kfraser@10418 583 case XENMEM_exchange:
kfraser@10418 584 rc = memory_exchange(guest_handle_cast(arg, xen_memory_exchange_t));
kfraser@10418 585 break;
kfraser@10418 586
kaf24@6486 587 case XENMEM_maximum_ram_page:
kaf24@7959 588 rc = max_page;
kaf24@7959 589 break;
kaf24@7959 590
kaf24@7959 591 case XENMEM_current_reservation:
kaf24@7959 592 case XENMEM_maximum_reservation:
kfraser@14471 593 case XENMEM_maximum_gpfn:
kaf24@9068 594 if ( copy_from_guest(&domid, arg, 1) )
kaf24@6486 595 return -EFAULT;
kaf24@7959 596
kaf24@9068 597 if ( likely(domid == DOMID_SELF) )
kaf24@7959 598 d = current->domain;
kaf24@7959 599 else if ( !IS_PRIV(current->domain) )
kaf24@7959 600 return -EPERM;
kfraser@14192 601 else if ( (d = rcu_lock_domain_by_id(domid)) == NULL )
kaf24@7959 602 return -ESRCH;
kaf24@7959 603
kfraser@15815 604 rc = xsm_memory_stat_reservation(current->domain, d);
kfraser@15815 605 if ( rc )
kfraser@15815 606 {
kfraser@15815 607 if ( domid != DOMID_SELF )
kfraser@15815 608 rcu_unlock_domain(d);
kfraser@15815 609 return rc;
kfraser@15815 610 }
kfraser@15815 611
kfraser@14471 612 switch ( op )
kfraser@14471 613 {
kfraser@14471 614 case XENMEM_current_reservation:
kfraser@14471 615 rc = d->tot_pages;
kfraser@14471 616 break;
kfraser@14471 617 case XENMEM_maximum_reservation:
kfraser@14471 618 rc = d->max_pages;
kfraser@14471 619 break;
kfraser@14471 620 default:
kfraser@14471 621 ASSERT(op == XENMEM_maximum_gpfn);
kfraser@14471 622 rc = domain_get_maximum_gpfn(d);
kfraser@14471 623 break;
kfraser@14471 624 }
kaf24@7959 625
kaf24@7959 626 if ( unlikely(domid != DOMID_SELF) )
kfraser@14192 627 rcu_unlock_domain(d);
kaf24@7959 628
kaf24@6486 629 break;
kaf24@6486 630
kaf24@8871 631 case XENMEM_translate_gpfn_list:
ack@13295 632 progress = cmd >> MEMOP_EXTENT_SHIFT;
kaf24@9068 633 rc = translate_gpfn_list(
kaf24@9068 634 guest_handle_cast(arg, xen_translate_gpfn_list_t),
kaf24@9068 635 &progress);
kaf24@8871 636 if ( rc == -EAGAIN )
kaf24@9068 637 return hypercall_create_continuation(
kaf24@9068 638 __HYPERVISOR_memory_op, "lh",
ack@13295 639 op | (progress << MEMOP_EXTENT_SHIFT), arg);
kaf24@8871 640 break;
kaf24@8871 641
kaf24@6486 642 default:
kaf24@8059 643 rc = arch_memory_op(op, arg);
kaf24@6486 644 break;
kaf24@6486 645 }
kaf24@6486 646
kaf24@6486 647 return rc;
kaf24@6486 648 }
kaf24@6486 649
kaf24@6486 650 /*
kaf24@6486 651 * Local variables:
kaf24@6486 652 * mode: C
kaf24@6486 653 * c-set-style: "BSD"
kaf24@6486 654 * c-basic-offset: 4
kaf24@6486 655 * tab-width: 4
kaf24@6486 656 * indent-tabs-mode: nil
kaf24@6486 657 * End:
kaf24@6486 658 */