ia64/xen-unstable

view xen/common/memory.c @ 19835:edfdeb150f27

Fix buildsystem to detect udev > version 124

udev removed the udevinfo symlink from versions higher than 123 and
xen's build-system could not detect if udev is in place and has the
required version.

Signed-off-by: Marc-A. Dahlhaus <mad@wol.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 25 13:02:37 2009 +0100 (2009-06-25)
parents 2d68d518038b
children
line source
1 /******************************************************************************
2 * memory.c
3 *
4 * Code to handle memory-related requests.
5 *
6 * Copyright (c) 2003-2004, B Dragovic
7 * Copyright (c) 2003-2005, K A Fraser
8 */
10 #include <xen/config.h>
11 #include <xen/types.h>
12 #include <xen/lib.h>
13 #include <xen/mm.h>
14 #include <xen/perfc.h>
15 #include <xen/sched.h>
16 #include <xen/event.h>
17 #include <xen/paging.h>
18 #include <xen/iocap.h>
19 #include <xen/guest_access.h>
20 #include <xen/hypercall.h>
21 #include <xen/errno.h>
22 #include <asm/current.h>
23 #include <asm/hardirq.h>
24 #include <xen/numa.h>
25 #include <public/memory.h>
26 #include <xsm/xsm.h>
28 struct memop_args {
29 /* INPUT */
30 struct domain *domain; /* Domain to be affected. */
31 XEN_GUEST_HANDLE(xen_pfn_t) extent_list; /* List of extent base addrs. */
32 unsigned int nr_extents; /* Number of extents to allocate or free. */
33 unsigned int extent_order; /* Size of each extent. */
34 unsigned int memflags; /* Allocation flags. */
36 /* INPUT/OUTPUT */
37 unsigned int nr_done; /* Number of extents processed so far. */
38 int preempted; /* Was the hypercall preempted? */
39 };
41 static void increase_reservation(struct memop_args *a)
42 {
43 struct page_info *page;
44 unsigned long i;
45 xen_pfn_t mfn;
46 struct domain *d = a->domain;
48 if ( !guest_handle_is_null(a->extent_list) &&
49 !guest_handle_subrange_okay(a->extent_list, a->nr_done,
50 a->nr_extents-1) )
51 return;
53 if ( !multipage_allocation_permitted(current->domain, a->extent_order) )
54 return;
56 for ( i = a->nr_done; i < a->nr_extents; i++ )
57 {
58 if ( hypercall_preempt_check() )
59 {
60 a->preempted = 1;
61 goto out;
62 }
64 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
65 if ( unlikely(page == NULL) )
66 {
67 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
68 "id=%d memflags=%x (%ld of %d)\n",
69 a->extent_order, d->domain_id, a->memflags,
70 i, a->nr_extents);
71 goto out;
72 }
74 /* Inform the domain of the new page's machine address. */
75 if ( !guest_handle_is_null(a->extent_list) )
76 {
77 mfn = page_to_mfn(page);
78 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
79 goto out;
80 }
81 }
83 out:
84 a->nr_done = i;
85 }
87 static void populate_physmap(struct memop_args *a)
88 {
89 struct page_info *page;
90 unsigned long i, j;
91 xen_pfn_t gpfn, mfn;
92 struct domain *d = a->domain;
94 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
95 a->nr_extents-1) )
96 return;
98 if ( !multipage_allocation_permitted(current->domain, a->extent_order) )
99 return;
101 for ( i = a->nr_done; i < a->nr_extents; i++ )
102 {
103 if ( hypercall_preempt_check() )
104 {
105 a->preempted = 1;
106 goto out;
107 }
109 if ( unlikely(__copy_from_guest_offset(&gpfn, a->extent_list, i, 1)) )
110 goto out;
112 if ( a->memflags & MEMF_populate_on_demand )
113 {
114 if ( guest_physmap_mark_populate_on_demand(d, gpfn,
115 a->extent_order) < 0 )
116 goto out;
117 }
118 else
119 {
120 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
121 if ( unlikely(page == NULL) )
122 {
123 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
124 "id=%d memflags=%x (%ld of %d)\n",
125 a->extent_order, d->domain_id, a->memflags,
126 i, a->nr_extents);
127 goto out;
128 }
130 mfn = page_to_mfn(page);
131 guest_physmap_add_page(d, gpfn, mfn, a->extent_order);
133 if ( !paging_mode_translate(d) )
134 {
135 for ( j = 0; j < (1 << a->extent_order); j++ )
136 set_gpfn_from_mfn(mfn + j, gpfn + j);
138 /* Inform the domain of the new page's machine address. */
139 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
140 goto out;
141 }
142 }
143 }
145 out:
146 a->nr_done = i;
147 }
149 int guest_remove_page(struct domain *d, unsigned long gmfn)
150 {
151 struct page_info *page;
152 unsigned long mfn;
154 mfn = gmfn_to_mfn(d, gmfn);
155 if ( unlikely(!mfn_valid(mfn)) )
156 {
157 gdprintk(XENLOG_INFO, "Domain %u page number %lx invalid\n",
158 d->domain_id, gmfn);
159 return 0;
160 }
162 page = mfn_to_page(mfn);
163 if ( unlikely(!get_page(page, d)) )
164 {
165 gdprintk(XENLOG_INFO, "Bad page free for domain %u\n", d->domain_id);
166 return 0;
167 }
169 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
170 put_page_and_type(page);
172 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
173 put_page(page);
175 guest_physmap_remove_page(d, gmfn, mfn, 0);
177 put_page(page);
179 return 1;
180 }
182 static void decrease_reservation(struct memop_args *a)
183 {
184 unsigned long i, j;
185 xen_pfn_t gmfn;
187 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
188 a->nr_extents-1) )
189 return;
191 for ( i = a->nr_done; i < a->nr_extents; i++ )
192 {
193 if ( hypercall_preempt_check() )
194 {
195 a->preempted = 1;
196 goto out;
197 }
199 if ( unlikely(__copy_from_guest_offset(&gmfn, a->extent_list, i, 1)) )
200 goto out;
202 /* See if populate-on-demand wants to handle this */
203 if ( is_hvm_domain(a->domain)
204 && p2m_pod_decrease_reservation(a->domain, gmfn, a->extent_order) )
205 continue;
207 for ( j = 0; j < (1 << a->extent_order); j++ )
208 if ( !guest_remove_page(a->domain, gmfn + j) )
209 goto out;
210 }
212 out:
213 a->nr_done = i;
214 }
216 static long memory_exchange(XEN_GUEST_HANDLE(xen_memory_exchange_t) arg)
217 {
218 struct xen_memory_exchange exch;
219 PAGE_LIST_HEAD(in_chunk_list);
220 PAGE_LIST_HEAD(out_chunk_list);
221 unsigned long in_chunk_order, out_chunk_order;
222 xen_pfn_t gpfn, gmfn, mfn;
223 unsigned long i, j, k;
224 unsigned int node, memflags = 0;
225 long rc = 0;
226 struct domain *d;
227 struct page_info *page;
229 if ( copy_from_guest(&exch, arg, 1) )
230 return -EFAULT;
232 /* Various sanity checks. */
233 if ( (exch.nr_exchanged > exch.in.nr_extents) ||
234 /* Input and output domain identifiers match? */
235 (exch.in.domid != exch.out.domid) ||
236 /* Sizes of input and output lists do not overflow a long? */
237 ((~0UL >> exch.in.extent_order) < exch.in.nr_extents) ||
238 ((~0UL >> exch.out.extent_order) < exch.out.nr_extents) ||
239 /* Sizes of input and output lists match? */
240 ((exch.in.nr_extents << exch.in.extent_order) !=
241 (exch.out.nr_extents << exch.out.extent_order)) )
242 {
243 rc = -EINVAL;
244 goto fail_early;
245 }
247 /* Only privileged guests can allocate multi-page contiguous extents. */
248 if ( !multipage_allocation_permitted(current->domain,
249 exch.in.extent_order) ||
250 !multipage_allocation_permitted(current->domain,
251 exch.out.extent_order) )
252 {
253 rc = -EPERM;
254 goto fail_early;
255 }
257 if ( exch.in.extent_order <= exch.out.extent_order )
258 {
259 in_chunk_order = exch.out.extent_order - exch.in.extent_order;
260 out_chunk_order = 0;
261 }
262 else
263 {
264 in_chunk_order = 0;
265 out_chunk_order = exch.in.extent_order - exch.out.extent_order;
266 }
268 /*
269 * Only support exchange on calling domain right now. Otherwise there are
270 * tricky corner cases to consider (e.g., dying domain).
271 */
272 if ( unlikely(exch.in.domid != DOMID_SELF) )
273 {
274 rc = IS_PRIV(current->domain) ? -EINVAL : -EPERM;
275 goto fail_early;
276 }
277 d = current->domain;
279 memflags |= MEMF_bits(domain_clamp_alloc_bitsize(
280 d,
281 XENMEMF_get_address_bits(exch.out.mem_flags) ? :
282 (BITS_PER_LONG+PAGE_SHIFT)));
283 node = XENMEMF_get_node(exch.out.mem_flags);
284 if ( node == NUMA_NO_NODE )
285 node = domain_to_node(d);
286 memflags |= MEMF_node(node);
288 for ( i = (exch.nr_exchanged >> in_chunk_order);
289 i < (exch.in.nr_extents >> in_chunk_order);
290 i++ )
291 {
292 if ( hypercall_preempt_check() )
293 {
294 exch.nr_exchanged = i << in_chunk_order;
295 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
296 return -EFAULT;
297 return hypercall_create_continuation(
298 __HYPERVISOR_memory_op, "lh", XENMEM_exchange, arg);
299 }
301 /* Steal a chunk's worth of input pages from the domain. */
302 for ( j = 0; j < (1UL << in_chunk_order); j++ )
303 {
304 if ( unlikely(__copy_from_guest_offset(
305 &gmfn, exch.in.extent_start, (i<<in_chunk_order)+j, 1)) )
306 {
307 rc = -EFAULT;
308 goto fail;
309 }
311 for ( k = 0; k < (1UL << exch.in.extent_order); k++ )
312 {
313 mfn = gmfn_to_mfn(d, gmfn + k);
314 if ( unlikely(!mfn_valid(mfn)) )
315 {
316 rc = -EINVAL;
317 goto fail;
318 }
320 page = mfn_to_page(mfn);
322 if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
323 {
324 rc = -EINVAL;
325 goto fail;
326 }
328 page_list_add(page, &in_chunk_list);
329 }
330 }
332 /* Allocate a chunk's worth of anonymous output pages. */
333 for ( j = 0; j < (1UL << out_chunk_order); j++ )
334 {
335 page = alloc_domheap_pages(NULL, exch.out.extent_order, memflags);
336 if ( unlikely(page == NULL) )
337 {
338 rc = -ENOMEM;
339 goto fail;
340 }
342 page_list_add(page, &out_chunk_list);
343 }
345 /*
346 * Success! Beyond this point we cannot fail for this chunk.
347 */
349 /* Destroy final reference to each input page. */
350 while ( (page = page_list_remove_head(&in_chunk_list)) )
351 {
352 if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
353 BUG();
354 mfn = page_to_mfn(page);
355 guest_physmap_remove_page(d, mfn_to_gmfn(d, mfn), mfn, 0);
356 put_page(page);
357 }
359 /* Assign each output page to the domain. */
360 j = 0;
361 while ( (page = page_list_remove_head(&out_chunk_list)) )
362 {
363 if ( assign_pages(d, page, exch.out.extent_order,
364 MEMF_no_refcount) )
365 BUG();
367 /* Note that we ignore errors accessing the output extent list. */
368 (void)__copy_from_guest_offset(
369 &gpfn, exch.out.extent_start, (i<<out_chunk_order)+j, 1);
371 mfn = page_to_mfn(page);
372 guest_physmap_add_page(d, gpfn, mfn, exch.out.extent_order);
374 if ( !paging_mode_translate(d) )
375 {
376 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
377 set_gpfn_from_mfn(mfn + k, gpfn + k);
378 (void)__copy_to_guest_offset(
379 exch.out.extent_start, (i<<out_chunk_order)+j, &mfn, 1);
380 }
382 j++;
383 }
384 BUG_ON(j != (1UL << out_chunk_order));
385 }
387 exch.nr_exchanged = exch.in.nr_extents;
388 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
389 rc = -EFAULT;
390 return rc;
392 /*
393 * Failed a chunk! Free any partial chunk work. Tell caller how many
394 * chunks succeeded.
395 */
396 fail:
397 /* Reassign any input pages we managed to steal. */
398 while ( (page = page_list_remove_head(&in_chunk_list)) )
399 if ( assign_pages(d, page, 0, MEMF_no_refcount) )
400 BUG();
402 /* Free any output pages we managed to allocate. */
403 while ( (page = page_list_remove_head(&out_chunk_list)) )
404 free_domheap_pages(page, exch.out.extent_order);
406 exch.nr_exchanged = i << in_chunk_order;
408 fail_early:
409 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
410 rc = -EFAULT;
411 return rc;
412 }
414 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE(void) arg)
415 {
416 struct domain *d;
417 int rc, op;
418 unsigned int address_bits;
419 unsigned long start_extent;
420 struct xen_memory_reservation reservation;
421 struct memop_args args;
422 domid_t domid;
424 op = cmd & MEMOP_CMD_MASK;
426 switch ( op )
427 {
428 case XENMEM_increase_reservation:
429 case XENMEM_decrease_reservation:
430 case XENMEM_populate_physmap:
431 start_extent = cmd >> MEMOP_EXTENT_SHIFT;
433 if ( copy_from_guest(&reservation, arg, 1) )
434 return start_extent;
436 /* Is size too large for us to encode a continuation? */
437 if ( reservation.nr_extents > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
438 return start_extent;
440 if ( unlikely(start_extent > reservation.nr_extents) )
441 return start_extent;
443 args.extent_list = reservation.extent_start;
444 args.nr_extents = reservation.nr_extents;
445 args.extent_order = reservation.extent_order;
446 args.nr_done = start_extent;
447 args.preempted = 0;
448 args.memflags = 0;
450 address_bits = XENMEMF_get_address_bits(reservation.mem_flags);
451 if ( (address_bits != 0) &&
452 (address_bits < (get_order_from_pages(max_page) + PAGE_SHIFT)) )
453 {
454 if ( address_bits <= PAGE_SHIFT )
455 return start_extent;
456 args.memflags = MEMF_bits(address_bits);
457 }
459 args.memflags |= MEMF_node(XENMEMF_get_node(reservation.mem_flags));
461 if ( op == XENMEM_populate_physmap
462 && (reservation.mem_flags & XENMEMF_populate_on_demand) )
463 args.memflags |= MEMF_populate_on_demand;
465 if ( likely(reservation.domid == DOMID_SELF) )
466 {
467 d = rcu_lock_current_domain();
468 }
469 else
470 {
471 if ( (d = rcu_lock_domain_by_id(reservation.domid)) == NULL )
472 return start_extent;
473 if ( !IS_PRIV_FOR(current->domain, d) )
474 {
475 rcu_unlock_domain(d);
476 return start_extent;
477 }
478 }
479 args.domain = d;
481 rc = xsm_memory_adjust_reservation(current->domain, d);
482 if ( rc )
483 {
484 rcu_unlock_domain(d);
485 return rc;
486 }
488 switch ( op )
489 {
490 case XENMEM_increase_reservation:
491 increase_reservation(&args);
492 break;
493 case XENMEM_decrease_reservation:
494 decrease_reservation(&args);
495 break;
496 default: /* XENMEM_populate_physmap */
497 populate_physmap(&args);
498 break;
499 }
501 rcu_unlock_domain(d);
503 rc = args.nr_done;
505 if ( args.preempted )
506 return hypercall_create_continuation(
507 __HYPERVISOR_memory_op, "lh",
508 op | (rc << MEMOP_EXTENT_SHIFT), arg);
510 break;
512 case XENMEM_exchange:
513 rc = memory_exchange(guest_handle_cast(arg, xen_memory_exchange_t));
514 break;
516 case XENMEM_maximum_ram_page:
517 rc = max_page;
518 break;
520 case XENMEM_current_reservation:
521 case XENMEM_maximum_reservation:
522 case XENMEM_maximum_gpfn:
523 if ( copy_from_guest(&domid, arg, 1) )
524 return -EFAULT;
526 rc = rcu_lock_target_domain_by_id(domid, &d);
527 if ( rc )
528 return rc;
530 rc = xsm_memory_stat_reservation(current->domain, d);
531 if ( rc )
532 {
533 rcu_unlock_domain(d);
534 return rc;
535 }
537 switch ( op )
538 {
539 case XENMEM_current_reservation:
540 rc = d->tot_pages;
541 break;
542 case XENMEM_maximum_reservation:
543 rc = d->max_pages;
544 break;
545 default:
546 ASSERT(op == XENMEM_maximum_gpfn);
547 rc = domain_get_maximum_gpfn(d);
548 break;
549 }
551 rcu_unlock_domain(d);
553 break;
555 default:
556 rc = arch_memory_op(op, arg);
557 break;
558 }
560 return rc;
561 }
563 /*
564 * Local variables:
565 * mode: C
566 * c-set-style: "BSD"
567 * c-basic-offset: 4
568 * tab-width: 4
569 * indent-tabs-mode: nil
570 * End:
571 */