ia64/xen-unstable

view xen/common/memory.c @ 6756:f752e0c873a6

merge?
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 12:32:20 2005 +0000 (2005-09-12)
parents cdfa7dd00c44 05f36d13e30c
children 4d899a738d59 8ca0f98ba8e2
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/shadow.h>
18 #include <asm/current.h>
19 #include <asm/hardirq.h>
20 #include <public/memory.h>
22 static long
23 increase_reservation(
24 struct domain *d,
25 unsigned long *extent_list,
26 unsigned int nr_extents,
27 unsigned int extent_order,
28 unsigned int flags,
29 int *preempted)
30 {
31 struct pfn_info *page;
32 unsigned int i;
34 if ( (extent_list != NULL) &&
35 !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
36 return 0;
38 if ( (extent_order != 0) && !IS_CAPABLE_PHYSDEV(current->domain) )
39 {
40 DPRINTK("Only I/O-capable domains may allocate multi-page extents.\n");
41 return 0;
42 }
44 for ( i = 0; i < nr_extents; i++ )
45 {
46 if ( hypercall_preempt_check() )
47 {
48 *preempted = 1;
49 return i;
50 }
52 if ( unlikely((page = alloc_domheap_pages(
53 d, extent_order, flags)) == NULL) )
54 {
55 DPRINTK("Could not allocate order=%d extent: "
56 "id=%d flags=%x (%d of %d)\n",
57 extent_order, d->domain_id, flags, i, nr_extents);
58 return i;
59 }
61 /* Inform the domain of the new page's machine address. */
62 if ( (extent_list != NULL) &&
63 (__put_user(page_to_pfn(page), &extent_list[i]) != 0) )
64 return i;
65 }
67 return nr_extents;
68 }
70 static long
71 decrease_reservation(
72 struct domain *d,
73 unsigned long *extent_list,
74 unsigned int nr_extents,
75 unsigned int extent_order,
76 unsigned int flags,
77 int *preempted)
78 {
79 struct pfn_info *page;
80 unsigned long i, j, mpfn;
82 if ( !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
83 return 0;
85 for ( i = 0; i < nr_extents; i++ )
86 {
87 if ( hypercall_preempt_check() )
88 {
89 *preempted = 1;
90 return i;
91 }
93 if ( unlikely(__get_user(mpfn, &extent_list[i]) != 0) )
94 return i;
96 for ( j = 0; j < (1 << extent_order); j++ )
97 {
98 if ( unlikely((mpfn + j) >= max_page) )
99 {
100 DPRINTK("Domain %u page number out of range (%lx >= %lx)\n",
101 d->domain_id, mpfn + j, max_page);
102 return i;
103 }
105 page = &frame_table[mpfn + j];
106 if ( unlikely(!get_page(page, d)) )
107 {
108 DPRINTK("Bad page free for domain %u\n", d->domain_id);
109 return i;
110 }
112 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
113 put_page_and_type(page);
115 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
116 put_page(page);
118 shadow_sync_and_drop_references(d, page);
120 put_page(page);
121 }
122 }
124 return nr_extents;
125 }
127 /*
128 * To allow safe resume of do_memory_op() after preemption, we need to know
129 * at what point in the page list to resume. For this purpose I steal the
130 * high-order bits of the @cmd parameter, which are otherwise unused and zero.
131 */
132 #define START_EXTENT_SHIFT 4 /* cmd[:4] == start_extent */
134 long do_memory_op(int cmd, void *arg)
135 {
136 struct domain *d;
137 int rc, start_extent, op, flags = 0, preempted = 0;
138 struct xen_memory_reservation reservation;
140 op = cmd & ((1 << START_EXTENT_SHIFT) - 1);
142 switch ( op )
143 {
144 case XENMEM_increase_reservation:
145 case XENMEM_decrease_reservation:
146 if ( copy_from_user(&reservation, arg, sizeof(reservation)) )
147 return -EFAULT;
149 start_extent = cmd >> START_EXTENT_SHIFT;
150 if ( unlikely(start_extent > reservation.nr_extents) )
151 return -EINVAL;
153 if ( reservation.extent_start != NULL )
154 reservation.extent_start += start_extent;
155 reservation.nr_extents -= start_extent;
157 if ( (reservation.address_bits != 0) &&
158 (reservation.address_bits <
159 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
160 {
161 if ( reservation.address_bits < 31 )
162 return -ENOMEM;
163 flags = ALLOC_DOM_DMA;
164 }
166 if ( likely(reservation.domid == DOMID_SELF) )
167 d = current->domain;
168 else if ( !IS_PRIV(current->domain) )
169 return -EPERM;
170 else if ( (d = find_domain_by_id(reservation.domid)) == NULL )
171 return -ESRCH;
173 rc = ((op == XENMEM_increase_reservation) ?
174 increase_reservation : decrease_reservation)(
175 d,
176 reservation.extent_start,
177 reservation.nr_extents,
178 reservation.extent_order,
179 flags,
180 &preempted);
182 if ( unlikely(reservation.domid != DOMID_SELF) )
183 put_domain(d);
185 rc += start_extent;
187 if ( preempted )
188 return hypercall2_create_continuation(
189 __HYPERVISOR_memory_op, op | (rc << START_EXTENT_SHIFT), arg);
191 break;
193 case XENMEM_maximum_ram_page:
194 if ( put_user(max_page, (unsigned long *)arg) )
195 return -EFAULT;
196 rc = 0;
197 break;
199 default:
200 rc = -ENOSYS;
201 break;
202 }
204 return rc;
205 }
207 /*
208 * Local variables:
209 * mode: C
210 * c-set-style: "BSD"
211 * c-basic-offset: 4
212 * tab-width: 4
213 * indent-tabs-mode: nil
214 * End:
215 */