direct-io.hg

view xen/common/dom_mem_ops.c @ 5517:10e9028c8e3d

bitkeeper revision 1.1718.1.10 (42b7b19aqOS_1M8I4pIOFjiTPYWV-g)

Merge bk://xenbits.xensource.com/xen-unstable.bk
into spot.cl.cam.ac.uk:C:/Documents and Settings/iap10/xen-unstable.bk
author iap10@spot.cl.cam.ac.uk
date Tue Jun 21 06:20:10 2005 +0000 (2005-06-21)
parents 2d8e63df504a
children 51094fae410e b53a65034532
line source
1 /******************************************************************************
2 * dom_mem_ops.c
3 *
4 * Code to handle memory related requests from domains eg. balloon driver.
5 *
6 * Copyright (c) 2003-2004, B Dragovic & K A Fraser.
7 */
9 #include <xen/config.h>
10 #include <xen/types.h>
11 #include <xen/lib.h>
12 #include <xen/mm.h>
13 #include <xen/perfc.h>
14 #include <xen/sched.h>
15 #include <xen/event.h>
16 #include <xen/shadow.h>
17 #include <asm/current.h>
18 #include <asm/hardirq.h>
20 /*
21 * To allow safe resume of do_dom_mem_op() after preemption, we need to know
22 * at what point in the page list to resume. For this purpose I steal the
23 * high-order bits of the @op parameter, which are otherwise unused and zero.
24 */
25 #define START_EXTENT_SHIFT 4 /* op[:4] == start_extent */
27 #define PREEMPT_CHECK(_op) \
28 if ( hypercall_preempt_check() ) \
29 return hypercall5_create_continuation( \
30 __HYPERVISOR_dom_mem_op, \
31 (_op) | (i << START_EXTENT_SHIFT), \
32 extent_list, nr_extents, extent_order, \
33 (d == current->domain) ? DOMID_SELF : d->domain_id);
35 static long
36 alloc_dom_mem(struct domain *d,
37 unsigned long *extent_list,
38 unsigned long start_extent,
39 unsigned int nr_extents,
40 unsigned int extent_order)
41 {
42 struct pfn_info *page;
43 unsigned long i;
45 if ( (extent_list != NULL) &&
46 !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
47 return start_extent;
49 if ( (extent_order != 0) && !IS_CAPABLE_PHYSDEV(current->domain) )
50 {
51 DPRINTK("Only I/O-capable domains may allocate > order-0 memory.\n");
52 return start_extent;
53 }
55 for ( i = start_extent; i < nr_extents; i++ )
56 {
57 PREEMPT_CHECK(MEMOP_increase_reservation);
59 if ( unlikely((page = alloc_domheap_pages(d, extent_order)) == NULL) )
60 {
61 DPRINTK("Could not allocate a frame\n");
62 return i;
63 }
65 /* Inform the domain of the new page's machine address. */
66 if ( (extent_list != NULL) &&
67 (__put_user(page_to_pfn(page), &extent_list[i]) != 0) )
68 return i;
69 }
71 return i;
72 }
74 static long
75 free_dom_mem(struct domain *d,
76 unsigned long *extent_list,
77 unsigned long start_extent,
78 unsigned int nr_extents,
79 unsigned int extent_order)
80 {
81 struct pfn_info *page;
82 unsigned long i, j, mpfn;
84 if ( !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
85 return start_extent;
87 for ( i = start_extent; i < nr_extents; i++ )
88 {
89 PREEMPT_CHECK(MEMOP_decrease_reservation);
91 if ( unlikely(__get_user(mpfn, &extent_list[i]) != 0) )
92 return i;
94 for ( j = 0; j < (1 << extent_order); j++ )
95 {
96 if ( unlikely((mpfn + j) >= max_page) )
97 {
98 DPRINTK("Domain %u page number out of range (%lx >= %lx)\n",
99 d->domain_id, mpfn + j, max_page);
100 return i;
101 }
103 page = &frame_table[mpfn + j];
104 if ( unlikely(!get_page(page, d)) )
105 {
106 DPRINTK("Bad page free for domain %u\n", d->domain_id);
107 return i;
108 }
110 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
111 put_page_and_type(page);
113 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
114 put_page(page);
116 shadow_sync_and_drop_references(d, page);
118 put_page(page);
119 }
120 }
122 return i;
123 }
125 long
126 do_dom_mem_op(unsigned long op,
127 unsigned long *extent_list,
128 unsigned int nr_extents,
129 unsigned int extent_order,
130 domid_t domid)
131 {
132 struct domain *d;
133 unsigned long rc, start_extent;
135 /* Extract @start_extent from @op. */
136 start_extent = op >> START_EXTENT_SHIFT;
137 op &= (1 << START_EXTENT_SHIFT) - 1;
139 if ( unlikely(start_extent > nr_extents) )
140 return -EINVAL;
142 if ( likely(domid == DOMID_SELF) )
143 d = current->domain;
144 else if ( unlikely(!IS_PRIV(current->domain)) )
145 return -EPERM;
146 else if ( unlikely((d = find_domain_by_id(domid)) == NULL) )
147 return -ESRCH;
149 switch ( op )
150 {
151 case MEMOP_increase_reservation:
152 rc = alloc_dom_mem(
153 d, extent_list, start_extent, nr_extents, extent_order);
154 break;
155 case MEMOP_decrease_reservation:
156 rc = free_dom_mem(
157 d, extent_list, start_extent, nr_extents, extent_order);
158 break;
159 default:
160 rc = -ENOSYS;
161 break;
162 }
164 if ( unlikely(domid != DOMID_SELF) )
165 put_domain(d);
167 return rc;
168 }
170 /*
171 * Local variables:
172 * mode: C
173 * c-set-style: "BSD"
174 * c-basic-offset: 4
175 * tab-width: 4
176 * indent-tabs-mode: nil
177 * End:
178 */