ia64/xen-unstable

view xen/common/xencomm.c @ 15783:c93e2a822d6f

[xen, xencomm] xencomm multiple page support
Current implementation doesn't allow struct xencomm_desc::address
array to be more than single page. On IA64 it causes 64GB+ domain
creation failure. This patch generalizes xencomm to allow multipage

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author kfraser@localhost.localdomain
date Tue Aug 28 15:32:27 2007 +0100 (2007-08-28)
parents b1c3b9df7d9a
children f4a0804d18f9
line source
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * Copyright (C) IBM Corp. 2006
17 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Tristan Gingold <tristan.gingold@bull.net>
20 * Isaku Yamahata <yamahata@valinux.co.jp> multiple page support
21 */
23 #include <xen/config.h>
24 #include <xen/mm.h>
25 #include <xen/sched.h>
26 #include <xen/xencomm.h>
27 #include <public/xen.h>
28 #include <public/xencomm.h>
30 #undef DEBUG
31 #ifdef DEBUG
32 #define xc_dprintk(f, a...) printk("[xencomm]" f , ## a)
33 #else
34 #define xc_dprintk(f, a...) ((void)0)
35 #endif
37 static void*
38 xencomm_vaddr(unsigned long paddr, struct page_info *page)
39 {
40 return (void*)((paddr & ~PAGE_MASK) | (unsigned long)page_to_virt(page));
41 }
43 /* get_page() to prevent from another vcpu freeing the page */
44 static int
45 xencomm_get_page(unsigned long paddr, struct page_info **page)
46 {
47 unsigned long maddr = paddr_to_maddr(paddr);
48 if ( maddr == 0 )
49 return -EFAULT;
51 *page = maddr_to_page(maddr);
52 if ( get_page(*page, current->domain) == 0 )
53 {
54 if ( page_get_owner(*page) != current->domain )
55 {
56 /*
57 * This page might be a page granted by another domain, or
58 * this page is freed with decrease reservation hypercall at
59 * the same time.
60 */
61 gdprintk(XENLOG_WARNING,
62 "bad page is passed. paddr 0x%lx maddr 0x%lx\n",
63 paddr, maddr);
64 return -EFAULT;
65 }
67 /* Try again. */
68 cpu_relax();
69 return -EAGAIN;
70 }
72 return 0;
73 }
75 /* check if struct desc doesn't cross page boundry */
76 static int
77 xencomm_desc_cross_page_boundary(unsigned long paddr)
78 {
79 unsigned long offset = paddr & ~PAGE_MASK;
80 if ( offset > PAGE_SIZE - sizeof(struct xencomm_desc) )
81 return 1;
82 return 0;
83 }
85 struct xencomm_ctxt {
86 struct xencomm_desc __user *desc_in_paddr;
87 uint32_t nr_addrs;
89 struct page_info *page;
90 unsigned long *address;
91 };
93 static uint32_t
94 xencomm_ctxt_nr_addrs(const struct xencomm_ctxt *ctxt)
95 {
96 return ctxt->nr_addrs;
97 }
99 static unsigned long*
100 xencomm_ctxt_address(struct xencomm_ctxt *ctxt)
101 {
102 return ctxt->address;
103 }
105 static int
106 xencomm_ctxt_init(const void* handle, struct xencomm_ctxt *ctxt)
107 {
108 struct page_info *page;
109 struct xencomm_desc *desc;
110 int ret;
112 /* avoid unaligned access */
113 if ( (unsigned long)handle % __alignof__(*desc) != 0 )
114 return -EINVAL;
115 if ( xencomm_desc_cross_page_boundary((unsigned long)handle) )
116 return -EINVAL;
118 /* first we need to access the descriptor */
119 ret = xencomm_get_page((unsigned long)handle, &page);
120 if ( ret )
121 return ret;
123 desc = xencomm_vaddr((unsigned long)handle, page);
124 if ( desc->magic != XENCOMM_MAGIC )
125 {
126 printk("%s: error: %p magic was 0x%x\n", __func__, desc, desc->magic);
127 put_page(page);
128 return -EINVAL;
129 }
131 ctxt->nr_addrs = desc->nr_addrs; /* copy before use.
132 * It is possible for a guest domain to
133 * modify concurrently.
134 */
135 ctxt->desc_in_paddr = (struct xencomm_desc*)handle;
136 ctxt->page = page;
137 ctxt->address = &desc->address[0];
138 return 0;
139 }
141 static int
142 xencomm_ctxt_next(struct xencomm_ctxt *ctxt, int i)
143 {
144 BUG_ON(i >= ctxt->nr_addrs);
145 /* in i == 0 case, we already calculated in xecomm_addr_init() */
146 if ( i != 0 )
147 ctxt->address++;
149 /* When crossing page boundary, machine address must be calculated. */
150 if ( ((unsigned long)ctxt->address & ~PAGE_MASK) == 0 )
151 {
152 unsigned long paddr =
153 (unsigned long)&(ctxt->desc_in_paddr->address[i]);
154 struct page_info *page;
155 int ret;
157 ret = xencomm_get_page(paddr, &page);
158 if ( ret == 0 )
159 {
160 put_page(ctxt->page);
161 ctxt->page = page;
162 ctxt->address = xencomm_vaddr(paddr, page);
163 }
164 return ret;
165 }
166 return 0;
167 }
169 static void
170 xencomm_ctxt_done(struct xencomm_ctxt *ctxt)
171 {
172 put_page(ctxt->page);
173 }
175 static int
176 xencomm_copy_chunk_from(
177 unsigned long to, unsigned long paddr, unsigned int len)
178 {
179 struct page_info *page;
181 while (1)
182 {
183 int res;
184 res = xencomm_get_page(paddr, &page);
185 if ( res != 0 )
186 {
187 if ( res == -EAGAIN )
188 continue; /* Try again. */
189 return res;
190 }
191 xc_dprintk("%lx[%d] -> %lx\n",
192 (unsigned long)xencomm_vaddr(paddr, page), len, to);
194 memcpy((void *)to, xencomm_vaddr(paddr, page), len);
195 put_page(page);
196 return 0;
197 }
198 /* NOTREACHED */
199 }
201 static unsigned long
202 xencomm_inline_from_guest(
203 void *to, const void *from, unsigned int n, unsigned int skip)
204 {
205 unsigned long src_paddr = xencomm_inline_addr(from) + skip;
207 while ( n > 0 )
208 {
209 unsigned int chunksz, bytes;
211 chunksz = PAGE_SIZE - (src_paddr % PAGE_SIZE);
212 bytes = min(chunksz, n);
214 if ( xencomm_copy_chunk_from((unsigned long)to, src_paddr, bytes) )
215 return n;
216 src_paddr += bytes;
217 to += bytes;
218 n -= bytes;
219 }
221 /* Always successful. */
222 return 0;
223 }
225 /**
226 * xencomm_copy_from_guest: Copy a block of data from domain space.
227 * @to: Machine address.
228 * @from: Physical address to a xencomm buffer descriptor.
229 * @n: Number of bytes to copy.
230 * @skip: Number of bytes from the start to skip.
231 *
232 * Copy data from domain to hypervisor.
233 *
234 * Returns number of bytes that could not be copied.
235 * On success, this will be zero.
236 */
237 unsigned long
238 xencomm_copy_from_guest(
239 void *to, const void *from, unsigned int n, unsigned int skip)
240 {
241 struct xencomm_ctxt ctxt;
242 unsigned int from_pos = 0;
243 unsigned int to_pos = 0;
244 unsigned int i = 0;
246 if ( xencomm_is_inline(from) )
247 return xencomm_inline_from_guest(to, from, n, skip);
249 if ( xencomm_ctxt_init(from, &ctxt) )
250 return n;
252 /* Iterate through the descriptor, copying up to a page at a time */
253 while ( (to_pos < n) && (i < xencomm_ctxt_nr_addrs(&ctxt)) )
254 {
255 unsigned long src_paddr;
256 unsigned int pgoffset, chunksz, chunk_skip;
258 if ( xencomm_ctxt_next(&ctxt, i) )
259 goto out;
260 src_paddr = *xencomm_ctxt_address(&ctxt);
261 if ( src_paddr == XENCOMM_INVALID )
262 {
263 i++;
264 continue;
265 }
267 pgoffset = src_paddr % PAGE_SIZE;
268 chunksz = PAGE_SIZE - pgoffset;
270 chunk_skip = min(chunksz, skip);
271 from_pos += chunk_skip;
272 chunksz -= chunk_skip;
273 skip -= chunk_skip;
275 if ( skip == 0 && chunksz > 0 )
276 {
277 unsigned int bytes = min(chunksz, n - to_pos);
279 if ( xencomm_copy_chunk_from((unsigned long)to + to_pos,
280 src_paddr + chunk_skip, bytes) )
281 goto out;
282 from_pos += bytes;
283 to_pos += bytes;
284 }
286 i++;
287 }
289 out:
290 xencomm_ctxt_done(&ctxt);
291 return n - to_pos;
292 }
294 static int
295 xencomm_copy_chunk_to(
296 unsigned long paddr, unsigned long from, unsigned int len)
297 {
298 struct page_info *page;
300 while (1)
301 {
302 int res;
303 res = xencomm_get_page(paddr, &page);
304 if ( res != 0 )
305 {
306 if ( res == -EAGAIN )
307 continue; /* Try again. */
308 return res;
309 }
310 xc_dprintk("%lx[%d] -> %lx\n", from, len,
311 (unsigned long)xencomm_vaddr(paddr, page));
313 memcpy(xencomm_vaddr(paddr, page), (void *)from, len);
314 put_page(page);
315 return 0;
316 }
317 /* NOTREACHED */
318 }
320 static unsigned long
321 xencomm_inline_to_guest(
322 void *to, const void *from, unsigned int n, unsigned int skip)
323 {
324 unsigned long dest_paddr = xencomm_inline_addr(to) + skip;
326 while ( n > 0 )
327 {
328 unsigned int chunksz, bytes;
330 chunksz = PAGE_SIZE - (dest_paddr % PAGE_SIZE);
331 bytes = min(chunksz, n);
333 if ( xencomm_copy_chunk_to(dest_paddr, (unsigned long)from, bytes) )
334 return n;
335 dest_paddr += bytes;
336 from += bytes;
337 n -= bytes;
338 }
340 /* Always successful. */
341 return 0;
342 }
344 /**
345 * xencomm_copy_to_guest: Copy a block of data to domain space.
346 * @to: Physical address to xencomm buffer descriptor.
347 * @from: Machine address.
348 * @n: Number of bytes to copy.
349 * @skip: Number of bytes from the start to skip.
350 *
351 * Copy data from hypervisor to domain.
352 *
353 * Returns number of bytes that could not be copied.
354 * On success, this will be zero.
355 */
356 unsigned long
357 xencomm_copy_to_guest(
358 void *to, const void *from, unsigned int n, unsigned int skip)
359 {
360 struct xencomm_ctxt ctxt;
361 unsigned int from_pos = 0;
362 unsigned int to_pos = 0;
363 unsigned int i = 0;
365 if ( xencomm_is_inline(to) )
366 return xencomm_inline_to_guest(to, from, n, skip);
368 if ( xencomm_ctxt_init(to, &ctxt) )
369 return n;
371 /* Iterate through the descriptor, copying up to a page at a time */
372 while ( (from_pos < n) && (i < xencomm_ctxt_nr_addrs(&ctxt)) )
373 {
374 unsigned long dest_paddr;
375 unsigned int pgoffset, chunksz, chunk_skip;
377 if ( xencomm_ctxt_next(&ctxt, i) )
378 goto out;
379 dest_paddr = *xencomm_ctxt_address(&ctxt);
380 if ( dest_paddr == XENCOMM_INVALID )
381 {
382 i++;
383 continue;
384 }
386 pgoffset = dest_paddr % PAGE_SIZE;
387 chunksz = PAGE_SIZE - pgoffset;
389 chunk_skip = min(chunksz, skip);
390 to_pos += chunk_skip;
391 chunksz -= chunk_skip;
392 skip -= chunk_skip;
394 if ( skip == 0 && chunksz > 0 )
395 {
396 unsigned int bytes = min(chunksz, n - from_pos);
398 if ( xencomm_copy_chunk_to(dest_paddr + chunk_skip,
399 (unsigned long)from + from_pos, bytes) )
400 goto out;
401 from_pos += bytes;
402 to_pos += bytes;
403 }
405 i++;
406 }
408 out:
409 xencomm_ctxt_done(&ctxt);
410 return n - from_pos;
411 }
413 static int xencomm_inline_add_offset(void **handle, unsigned int bytes)
414 {
415 *handle += bytes;
416 return 0;
417 }
419 /* Offset page addresses in 'handle' to skip 'bytes' bytes. Set completely
420 * exhausted pages to XENCOMM_INVALID. */
421 int xencomm_add_offset(void **handle, unsigned int bytes)
422 {
423 struct xencomm_ctxt ctxt;
424 int i = 0;
425 int res = 0;
427 if ( xencomm_is_inline(*handle) )
428 return xencomm_inline_add_offset(handle, bytes);
430 res = xencomm_ctxt_init(handle, &ctxt);
431 if ( res != 0 )
432 return res;
434 /* Iterate through the descriptor incrementing addresses */
435 while ( (bytes > 0) && (i < xencomm_ctxt_nr_addrs(&ctxt)) )
436 {
437 unsigned long *address;
438 unsigned long dest_paddr;
439 unsigned int pgoffset, chunksz, chunk_skip;
441 res = xencomm_ctxt_next(&ctxt, i);
442 if ( res )
443 goto out;
444 address = xencomm_ctxt_address(&ctxt);
445 dest_paddr = *address;
446 if ( dest_paddr == XENCOMM_INVALID )
447 {
448 i++;
449 continue;
450 }
452 pgoffset = dest_paddr % PAGE_SIZE;
453 chunksz = PAGE_SIZE - pgoffset;
455 chunk_skip = min(chunksz, bytes);
456 if ( chunk_skip == chunksz )
457 *address = XENCOMM_INVALID; /* exhausted this page */
458 else
459 *address += chunk_skip;
460 bytes -= chunk_skip;
462 i++;
463 }
465 out:
466 xencomm_ctxt_done(&ctxt);
467 return res;
468 }
470 int xencomm_handle_is_null(void *handle)
471 {
472 struct xencomm_ctxt ctxt;
473 int i;
474 int res = 1;
476 if ( xencomm_is_inline(handle) )
477 return xencomm_inline_addr(handle) == 0;
479 if ( xencomm_ctxt_init(handle, &ctxt) )
480 return 1;
482 for ( i = 0; i < xencomm_ctxt_nr_addrs(&ctxt); i++ )
483 {
484 if ( xencomm_ctxt_next(&ctxt, i) )
485 goto out;
486 if ( *xencomm_ctxt_address(&ctxt) != XENCOMM_INVALID )
487 {
488 res = 0;
489 goto out;
490 }
491 }
493 out:
494 xencomm_ctxt_done(&ctxt);
495 return res;
496 }