ia64/xen-unstable

annotate xen/common/memory.c @ 8673:17dc21008351

New memory hypercall 'populate_physmap'. Accepts a list of
pseudophys frame numbers to be populated with memory.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jan 27 12:49:47 2006 +0100 (2006-01-27)
parents 4520b451a70e
children 990c009015e8
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>
kaf24@6486 17 #include <xen/shadow.h>
kaf24@8468 18 #include <xen/iocap.h>
kaf24@6486 19 #include <asm/current.h>
kaf24@6486 20 #include <asm/hardirq.h>
kaf24@6486 21 #include <public/memory.h>
kaf24@6486 22
kaf24@6486 23 static long
kaf24@6486 24 increase_reservation(
kaf24@6486 25 struct domain *d,
kaf24@6486 26 unsigned long *extent_list,
kaf24@6486 27 unsigned int nr_extents,
kaf24@6486 28 unsigned int extent_order,
kaf24@6607 29 unsigned int flags,
kaf24@6607 30 int *preempted)
kaf24@6486 31 {
kaf24@6486 32 struct pfn_info *page;
kaf24@8673 33 unsigned long i;
kaf24@6486 34
kaf24@6701 35 if ( (extent_list != NULL) &&
kaf24@6701 36 !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
kaf24@6486 37 return 0;
kaf24@6486 38
kaf24@8468 39 if ( (extent_order != 0) &&
kaf24@8468 40 !multipage_allocation_permitted(current->domain) )
kaf24@6486 41 return 0;
kaf24@6486 42
kaf24@6486 43 for ( i = 0; i < nr_extents; i++ )
kaf24@6486 44 {
kaf24@6486 45 if ( hypercall_preempt_check() )
kaf24@6607 46 {
kaf24@6607 47 *preempted = 1;
kaf24@6486 48 return i;
kaf24@6607 49 }
kaf24@6486 50
kaf24@6486 51 if ( unlikely((page = alloc_domheap_pages(
kaf24@6486 52 d, extent_order, flags)) == NULL) )
kaf24@6486 53 {
kaf24@6752 54 DPRINTK("Could not allocate order=%d extent: "
kaf24@8673 55 "id=%d flags=%x (%ld of %d)\n",
kaf24@6752 56 extent_order, d->domain_id, flags, i, nr_extents);
kaf24@6486 57 return i;
kaf24@6486 58 }
kaf24@6486 59
kaf24@6486 60 /* Inform the domain of the new page's machine address. */
kaf24@6701 61 if ( (extent_list != NULL) &&
kaf24@6701 62 (__put_user(page_to_pfn(page), &extent_list[i]) != 0) )
kaf24@6486 63 return i;
kaf24@6486 64 }
kaf24@6486 65
kaf24@6486 66 return nr_extents;
kaf24@6486 67 }
kaf24@6486 68
kaf24@6486 69 static long
kaf24@8673 70 populate_physmap(
kaf24@8673 71 struct domain *d,
kaf24@8673 72 unsigned long *extent_list,
kaf24@8673 73 unsigned int nr_extents,
kaf24@8673 74 unsigned int extent_order,
kaf24@8673 75 unsigned int flags,
kaf24@8673 76 int *preempted)
kaf24@8673 77 {
kaf24@8673 78 struct pfn_info *page;
kaf24@8673 79 unsigned long i, j, pfn, mfn;
kaf24@8673 80
kaf24@8673 81 if ( !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
kaf24@8673 82 return 0;
kaf24@8673 83
kaf24@8673 84 if ( (extent_order != 0) &&
kaf24@8673 85 !multipage_allocation_permitted(current->domain) )
kaf24@8673 86 return 0;
kaf24@8673 87
kaf24@8673 88 for ( i = 0; i < nr_extents; i++ )
kaf24@8673 89 {
kaf24@8673 90 if ( hypercall_preempt_check() )
kaf24@8673 91 {
kaf24@8673 92 *preempted = 1;
kaf24@8673 93 return i;
kaf24@8673 94 }
kaf24@8673 95
kaf24@8673 96 if ( unlikely((page = alloc_domheap_pages(
kaf24@8673 97 d, extent_order, flags)) == NULL) )
kaf24@8673 98 {
kaf24@8673 99 DPRINTK("Could not allocate order=%d extent: "
kaf24@8673 100 "id=%d flags=%x (%ld of %d)\n",
kaf24@8673 101 extent_order, d->domain_id, flags, i, nr_extents);
kaf24@8673 102 return i;
kaf24@8673 103 }
kaf24@8673 104
kaf24@8673 105 mfn = page_to_pfn(page);
kaf24@8673 106
kaf24@8673 107 if ( unlikely(__get_user(pfn, &extent_list[i]) != 0) )
kaf24@8673 108 return i;
kaf24@8673 109
kaf24@8673 110 for ( j = 0; j < (1 << extent_order); j++ )
kaf24@8673 111 set_pfn_from_mfn(mfn + j, pfn + j);
kaf24@8673 112
kaf24@8673 113 /* Inform the domain of the new page's machine address. */
kaf24@8673 114 if ( __put_user(mfn, &extent_list[i]) != 0 )
kaf24@8673 115 return i;
kaf24@8673 116 }
kaf24@8673 117
kaf24@8673 118 return nr_extents;
kaf24@8673 119 }
kaf24@8673 120
kaf24@8673 121 static long
kaf24@6486 122 decrease_reservation(
kaf24@6486 123 struct domain *d,
kaf24@6486 124 unsigned long *extent_list,
kaf24@6486 125 unsigned int nr_extents,
kaf24@6486 126 unsigned int extent_order,
kaf24@6607 127 unsigned int flags,
kaf24@6607 128 int *preempted)
kaf24@6486 129 {
kaf24@6486 130 struct pfn_info *page;
kaf24@8673 131 unsigned long i, j, mfn;
kaf24@6486 132
kaf24@6486 133 if ( !array_access_ok(extent_list, nr_extents, sizeof(*extent_list)) )
kaf24@6486 134 return 0;
kaf24@6486 135
kaf24@6486 136 for ( i = 0; i < nr_extents; i++ )
kaf24@6486 137 {
kaf24@6486 138 if ( hypercall_preempt_check() )
kaf24@6607 139 {
kaf24@6607 140 *preempted = 1;
kaf24@6486 141 return i;
kaf24@6607 142 }
kaf24@6486 143
kaf24@8673 144 if ( unlikely(__get_user(mfn, &extent_list[i]) != 0) )
kaf24@6486 145 return i;
kaf24@6486 146
kaf24@6486 147 for ( j = 0; j < (1 << extent_order); j++ )
kaf24@6486 148 {
kaf24@8673 149 if ( unlikely((mfn + j) >= max_page) )
kaf24@6486 150 {
kaf24@6486 151 DPRINTK("Domain %u page number out of range (%lx >= %lx)\n",
kaf24@8673 152 d->domain_id, mfn + j, max_page);
kaf24@6486 153 return i;
kaf24@6486 154 }
kaf24@6486 155
kaf24@8673 156 page = pfn_to_page(mfn + j);
kaf24@6486 157 if ( unlikely(!get_page(page, d)) )
kaf24@6486 158 {
kaf24@6486 159 DPRINTK("Bad page free for domain %u\n", d->domain_id);
kaf24@6486 160 return i;
kaf24@6486 161 }
kaf24@6486 162
kaf24@6486 163 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
kaf24@6486 164 put_page_and_type(page);
kaf24@6486 165
kaf24@6486 166 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
kaf24@6486 167 put_page(page);
kaf24@6486 168
kaf24@6486 169 shadow_sync_and_drop_references(d, page);
kaf24@6486 170
kaf24@6486 171 put_page(page);
kaf24@6486 172 }
kaf24@6486 173 }
kaf24@6486 174
kaf24@6486 175 return nr_extents;
kaf24@6486 176 }
kaf24@6486 177
kaf24@6486 178 /*
kaf24@6486 179 * To allow safe resume of do_memory_op() after preemption, we need to know
kaf24@6486 180 * at what point in the page list to resume. For this purpose I steal the
kaf24@6486 181 * high-order bits of the @cmd parameter, which are otherwise unused and zero.
kaf24@6486 182 */
kaf24@6486 183 #define START_EXTENT_SHIFT 4 /* cmd[:4] == start_extent */
kaf24@6486 184
kaf24@6486 185 long do_memory_op(int cmd, void *arg)
kaf24@6486 186 {
kaf24@6486 187 struct domain *d;
kaf24@6607 188 int rc, start_extent, op, flags = 0, preempted = 0;
kaf24@6486 189 struct xen_memory_reservation reservation;
kaf24@7959 190 domid_t domid;
kaf24@6486 191
kaf24@6486 192 op = cmd & ((1 << START_EXTENT_SHIFT) - 1);
kaf24@6486 193
kaf24@6486 194 switch ( op )
kaf24@6486 195 {
kaf24@6486 196 case XENMEM_increase_reservation:
kaf24@6486 197 case XENMEM_decrease_reservation:
kaf24@8673 198 case XENMEM_populate_physmap:
kaf24@6486 199 if ( copy_from_user(&reservation, arg, sizeof(reservation)) )
kaf24@6486 200 return -EFAULT;
kaf24@6486 201
kaf24@6486 202 start_extent = cmd >> START_EXTENT_SHIFT;
kaf24@6486 203 if ( unlikely(start_extent > reservation.nr_extents) )
kaf24@6486 204 return -EINVAL;
kaf24@6486 205
kaf24@6486 206 if ( reservation.extent_start != NULL )
kaf24@6486 207 reservation.extent_start += start_extent;
kaf24@6486 208 reservation.nr_extents -= start_extent;
kaf24@6486 209
kaf24@6701 210 if ( (reservation.address_bits != 0) &&
kaf24@6702 211 (reservation.address_bits <
kaf24@6702 212 (get_order_from_pages(max_page) + PAGE_SHIFT)) )
kaf24@6486 213 {
kaf24@6486 214 if ( reservation.address_bits < 31 )
kaf24@6486 215 return -ENOMEM;
kaf24@6486 216 flags = ALLOC_DOM_DMA;
kaf24@6486 217 }
kaf24@6486 218
kaf24@6486 219 if ( likely(reservation.domid == DOMID_SELF) )
kaf24@6486 220 d = current->domain;
kaf24@6486 221 else if ( !IS_PRIV(current->domain) )
kaf24@6486 222 return -EPERM;
kaf24@6486 223 else if ( (d = find_domain_by_id(reservation.domid)) == NULL )
kaf24@6486 224 return -ESRCH;
kaf24@6486 225
kaf24@8673 226 switch ( op )
kaf24@8673 227 {
kaf24@8673 228 case XENMEM_increase_reservation:
kaf24@8673 229 rc = increase_reservation(
kaf24@8673 230 d,
kaf24@8673 231 reservation.extent_start,
kaf24@8673 232 reservation.nr_extents,
kaf24@8673 233 reservation.extent_order,
kaf24@8673 234 flags,
kaf24@8673 235 &preempted);
kaf24@8673 236 break;
kaf24@8673 237 case XENMEM_decrease_reservation:
kaf24@8673 238 rc = decrease_reservation(
kaf24@8673 239 d,
kaf24@8673 240 reservation.extent_start,
kaf24@8673 241 reservation.nr_extents,
kaf24@8673 242 reservation.extent_order,
kaf24@8673 243 flags,
kaf24@8673 244 &preempted);
kaf24@8673 245 break;
kaf24@8673 246 case XENMEM_populate_physmap:
kaf24@8673 247 default:
kaf24@8673 248 rc = populate_physmap(
kaf24@8673 249 d,
kaf24@8673 250 reservation.extent_start,
kaf24@8673 251 reservation.nr_extents,
kaf24@8673 252 reservation.extent_order,
kaf24@8673 253 flags,
kaf24@8673 254 &preempted);
kaf24@8673 255 break;
kaf24@8673 256 }
kaf24@6486 257
kaf24@6486 258 if ( unlikely(reservation.domid != DOMID_SELF) )
kaf24@6486 259 put_domain(d);
kaf24@6486 260
kaf24@6486 261 rc += start_extent;
kaf24@6486 262
kaf24@6607 263 if ( preempted )
kaf24@6486 264 return hypercall2_create_continuation(
kaf24@6607 265 __HYPERVISOR_memory_op, op | (rc << START_EXTENT_SHIFT), arg);
kaf24@6607 266
kaf24@6486 267 break;
kaf24@6486 268
kaf24@6486 269 case XENMEM_maximum_ram_page:
kaf24@7959 270 rc = max_page;
kaf24@7959 271 break;
kaf24@7959 272
kaf24@7959 273 case XENMEM_current_reservation:
kaf24@7959 274 case XENMEM_maximum_reservation:
kaf24@7959 275 if ( get_user(domid, (domid_t *)arg) )
kaf24@6486 276 return -EFAULT;
kaf24@7959 277
kaf24@7959 278 if ( likely((domid = (unsigned long)arg) == DOMID_SELF) )
kaf24@7959 279 d = current->domain;
kaf24@7959 280 else if ( !IS_PRIV(current->domain) )
kaf24@7959 281 return -EPERM;
kaf24@7959 282 else if ( (d = find_domain_by_id(domid)) == NULL )
kaf24@7959 283 return -ESRCH;
kaf24@7959 284
kaf24@7959 285 rc = (op == XENMEM_current_reservation) ? d->tot_pages : d->max_pages;
kaf24@7959 286
kaf24@7959 287 if ( unlikely(domid != DOMID_SELF) )
kaf24@7959 288 put_domain(d);
kaf24@7959 289
kaf24@6486 290 break;
kaf24@6486 291
kaf24@6486 292 default:
kaf24@8059 293 rc = arch_memory_op(op, arg);
kaf24@6486 294 break;
kaf24@6486 295 }
kaf24@6486 296
kaf24@6486 297 return rc;
kaf24@6486 298 }
kaf24@6486 299
kaf24@6486 300 /*
kaf24@6486 301 * Local variables:
kaf24@6486 302 * mode: C
kaf24@6486 303 * c-set-style: "BSD"
kaf24@6486 304 * c-basic-offset: 4
kaf24@6486 305 * tab-width: 4
kaf24@6486 306 * indent-tabs-mode: nil
kaf24@6486 307 * End:
kaf24@6486 308 */