ia64/xen-unstable

view xen/arch/powerpc/usercopy.c @ 12932:0379ac3367b2

[XEN][POWERPC] Use gmfn_to_mfn() over pfn2mfn()
Should only use pfn2mfn() if you care about the type of memory.
Signed-off-by: Jimi Xenidis <jimix@watson.ibm.com>
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author Jimi Xenidis <jimix@watson.ibm.com>
date Mon Oct 02 21:40:26 2006 -0400 (2006-10-02)
parents 4da585fb62f9
children a510c94ceaa3
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 */
21 #include <xen/config.h>
22 #include <xen/mm.h>
23 #include <xen/sched.h>
24 #include <asm/current.h>
25 #include <asm/uaccess.h>
26 #include <asm/debugger.h>
27 #include <public/xen.h>
28 #include <public/xencomm.h>
30 #undef DEBUG
31 #ifdef DEBUG
32 static int xencomm_debug = 1; /* extremely verbose */
33 #else
34 #define xencomm_debug 0
35 #endif
37 /* XXX need to return error, not panic, if domain passed a bad pointer */
38 static unsigned long paddr_to_maddr(unsigned long paddr)
39 {
40 struct vcpu *v = get_current();
41 struct domain *d = v->domain;
42 ulong gpfn;
43 ulong offset;
44 ulong pa = paddr;
46 offset = pa & ~PAGE_MASK;
47 gpfn = pa >> PAGE_SHIFT;
49 pa = gmfn_to_mfn(d, gpfn);
50 if (pa == INVALID_MFN) {
51 printk("%s: Dom:%d bad paddr: 0x%lx\n",
52 __func__, d->domain_id, paddr);
53 return 0;
54 }
56 pa <<= PAGE_SHIFT;
57 pa |= offset;
59 return pa;
60 }
62 /**
63 * xencomm_copy_from_guest: Copy a block of data from domain space.
64 * @to: Machine address.
65 * @from: Physical address to a xencomm buffer descriptor.
66 * @n: Number of bytes to copy.
67 * @skip: Number of bytes from the start to skip.
68 *
69 * Copy data from domain to hypervisor.
70 *
71 * Returns number of bytes that could not be copied.
72 * On success, this will be zero.
73 */
74 unsigned long
75 xencomm_copy_from_guest(void *to, const void *from, unsigned int n,
76 unsigned int skip)
77 {
78 struct xencomm_desc *desc;
79 unsigned int from_pos = 0;
80 unsigned int to_pos = 0;
81 unsigned int i = 0;
83 /* first we need to access the descriptor */
84 desc = (struct xencomm_desc *)paddr_to_maddr((unsigned long)from);
85 if (desc == NULL)
86 return n;
88 if (desc->magic != XENCOMM_MAGIC) {
89 printk("%s: error: %p magic was 0x%x\n",
90 __func__, desc, desc->magic);
91 return n;
92 }
94 /* iterate through the descriptor, copying up to a page at a time */
95 while ((to_pos < n) && (i < desc->nr_addrs)) {
96 unsigned long src_paddr = desc->address[i];
97 unsigned int pgoffset;
98 unsigned int chunksz;
99 unsigned int chunk_skip;
101 if (src_paddr == XENCOMM_INVALID) {
102 i++;
103 continue;
104 }
106 pgoffset = src_paddr % PAGE_SIZE;
107 chunksz = PAGE_SIZE - pgoffset;
109 chunk_skip = min(chunksz, skip);
110 from_pos += chunk_skip;
111 chunksz -= chunk_skip;
112 skip -= chunk_skip;
114 if (skip == 0) {
115 unsigned long src_maddr;
116 unsigned long dest = (unsigned long)to + to_pos;
117 unsigned int bytes = min(chunksz, n - to_pos);
119 src_maddr = paddr_to_maddr(src_paddr + chunk_skip);
120 if (src_maddr == 0)
121 return n - to_pos;
123 if (xencomm_debug)
124 printk("%lx[%d] -> %lx\n", src_maddr, bytes, dest);
125 memcpy((void *)dest, (void *)src_maddr, bytes);
126 from_pos += bytes;
127 to_pos += bytes;
128 }
130 i++;
131 }
133 return n - to_pos;
134 }
136 /**
137 * xencomm_copy_to_guest: Copy a block of data to domain space.
138 * @to: Physical address to xencomm buffer descriptor.
139 * @from: Machine address.
140 * @n: Number of bytes to copy.
141 * @skip: Number of bytes from the start to skip.
142 *
143 * Copy data from hypervisor to domain.
144 *
145 * Returns number of bytes that could not be copied.
146 * On success, this will be zero.
147 */
148 unsigned long
149 xencomm_copy_to_guest(void *to, const void *from, unsigned int n,
150 unsigned int skip)
151 {
152 struct xencomm_desc *desc;
153 unsigned int from_pos = 0;
154 unsigned int to_pos = 0;
155 unsigned int i = 0;
157 /* first we need to access the descriptor */
158 desc = (struct xencomm_desc *)paddr_to_maddr((unsigned long)to);
159 if (desc == NULL)
160 return n;
162 if (desc->magic != XENCOMM_MAGIC) {
163 printk("%s error: %p magic was 0x%x\n", __func__, desc, desc->magic);
164 return n;
165 }
167 /* iterate through the descriptor, copying up to a page at a time */
168 while ((from_pos < n) && (i < desc->nr_addrs)) {
169 unsigned long dest_paddr = desc->address[i];
170 unsigned int pgoffset;
171 unsigned int chunksz;
172 unsigned int chunk_skip;
174 if (dest_paddr == XENCOMM_INVALID) {
175 i++;
176 continue;
177 }
179 pgoffset = dest_paddr % PAGE_SIZE;
180 chunksz = PAGE_SIZE - pgoffset;
182 chunk_skip = min(chunksz, skip);
183 to_pos += chunk_skip;
184 chunksz -= chunk_skip;
185 skip -= chunk_skip;
187 if (skip == 0) {
188 unsigned long dest_maddr;
189 unsigned long source = (unsigned long)from + from_pos;
190 unsigned int bytes = min(chunksz, n - from_pos);
192 dest_maddr = paddr_to_maddr(dest_paddr + chunk_skip);
193 if (dest_maddr == 0)
194 return -1;
196 if (xencomm_debug)
197 printk("%lx[%d] -> %lx\n", source, bytes, dest_maddr);
198 memcpy((void *)dest_maddr, (void *)source, bytes);
199 from_pos += bytes;
200 to_pos += bytes;
201 }
203 i++;
204 }
206 return n - from_pos;
207 }
209 /* Offset page addresses in 'handle' to skip 'bytes' bytes. Set completely
210 * exhausted pages to XENCOMM_INVALID. */
211 int xencomm_add_offset(void *handle, unsigned int bytes)
212 {
213 struct xencomm_desc *desc;
214 int i = 0;
216 /* first we need to access the descriptor */
217 desc = (struct xencomm_desc *)paddr_to_maddr((unsigned long)handle);
218 if (desc == NULL)
219 return -1;
221 if (desc->magic != XENCOMM_MAGIC) {
222 printk("%s error: %p magic was 0x%x\n", __func__, desc, desc->magic);
223 return -1;
224 }
226 /* iterate through the descriptor incrementing addresses */
227 while ((bytes > 0) && (i < desc->nr_addrs)) {
228 unsigned long dest_paddr = desc->address[i];
229 unsigned int pgoffset;
230 unsigned int chunksz;
231 unsigned int chunk_skip;
233 if (dest_paddr == XENCOMM_INVALID) {
234 i++;
235 continue;
236 }
238 pgoffset = dest_paddr % PAGE_SIZE;
239 chunksz = PAGE_SIZE - pgoffset;
241 chunk_skip = min(chunksz, bytes);
242 if (chunk_skip == chunksz) {
243 /* exhausted this page */
244 desc->address[i] = XENCOMM_INVALID;
245 } else {
246 desc->address[i] += chunk_skip;
247 }
248 bytes -= chunk_skip;
250 i++;
251 }
252 return 0;
253 }
255 int xencomm_handle_is_null(void *ptr)
256 {
257 struct xencomm_desc *desc;
259 desc = (struct xencomm_desc *)paddr_to_maddr((unsigned long)ptr);
260 if (desc == NULL)
261 return 1;
263 return (desc->nr_addrs == 0);
264 }