ia64/xen-unstable

annotate xen/common/memory.c @ 15635:3c28bc13a3f8

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