xen-vtx-unstable
annotate linux-2.6.11-xen-sparse/arch/xen/kernel/gnttab.c @ 5272:cefe173ea09b
bitkeeper revision 1.1638 (429f0702i8Aq9b8ywQxXo9xS63DS0A)
missing braces for the for body (this is not python)
Signed-off-by: Vincent Hanquez <vincent@xensource.com>
missing braces for the for body (this is not python)
Signed-off-by: Vincent Hanquez <vincent@xensource.com>
author | vh249@arcadians.cl.cam.ac.uk |
---|---|
date | Thu Jun 02 13:17:54 2005 +0000 (2005-06-02) |
parents | 39bfbd5ae9b8 |
children | 11dce3390a66 |
rev | line source |
---|---|
cl349@4693 | 1 /****************************************************************************** |
cl349@4693 | 2 * gnttab.c |
cl349@4693 | 3 * |
cl349@4693 | 4 * Two sets of functionality: |
cl349@4693 | 5 * 1. Granting foreign access to our memory reservation. |
cl349@4693 | 6 * 2. Accessing others' memory reservations via grant references. |
cl349@4693 | 7 * (i.e., mechanisms for both sender and recipient of grant references) |
cl349@4693 | 8 * |
cl349@4693 | 9 * Copyright (c) 2005, Christopher Clark |
cl349@4693 | 10 * Copyright (c) 2004, K A Fraser |
cl349@4693 | 11 */ |
cl349@4693 | 12 |
cl349@4693 | 13 #include <linux/config.h> |
cl349@4693 | 14 #include <linux/module.h> |
cl349@4693 | 15 #include <linux/sched.h> |
cl349@4693 | 16 #include <asm/pgtable.h> |
cl349@4693 | 17 #include <asm/fixmap.h> |
cl349@4693 | 18 #include <asm/uaccess.h> |
cl349@4693 | 19 #include <asm-xen/xen_proc.h> |
cl349@4693 | 20 #include <asm-xen/linux-public/privcmd.h> |
cl349@4693 | 21 #include <asm-xen/gnttab.h> |
cl349@4693 | 22 |
cl349@4693 | 23 #if 1 |
cl349@4693 | 24 #define ASSERT(_p) \ |
cl349@4693 | 25 if ( !(_p) ) { printk(KERN_ALERT"Assertion '%s': line %d, file %s\n", \ |
cl349@4693 | 26 #_p , __LINE__, __FILE__); *(int*)0=0; } |
cl349@4693 | 27 #else |
cl349@4693 | 28 #define ASSERT(_p) ((void)0) |
cl349@4693 | 29 #endif |
cl349@4693 | 30 |
cl349@4693 | 31 #define WPRINTK(fmt, args...) \ |
cl349@4693 | 32 printk(KERN_WARNING "xen_grant: " fmt, ##args) |
cl349@4693 | 33 |
cl349@4693 | 34 |
cl349@4693 | 35 EXPORT_SYMBOL(gnttab_grant_foreign_access); |
cl349@4693 | 36 EXPORT_SYMBOL(gnttab_end_foreign_access); |
cl349@4693 | 37 EXPORT_SYMBOL(gnttab_query_foreign_access); |
cl349@4693 | 38 EXPORT_SYMBOL(gnttab_grant_foreign_transfer); |
cl349@4693 | 39 EXPORT_SYMBOL(gnttab_end_foreign_transfer); |
cl349@4693 | 40 EXPORT_SYMBOL(gnttab_alloc_grant_references); |
cl349@4693 | 41 EXPORT_SYMBOL(gnttab_free_grant_references); |
cl349@4693 | 42 EXPORT_SYMBOL(gnttab_claim_grant_reference); |
cl349@4693 | 43 EXPORT_SYMBOL(gnttab_release_grant_reference); |
cl349@4693 | 44 EXPORT_SYMBOL(gnttab_grant_foreign_access_ref); |
cl349@4693 | 45 EXPORT_SYMBOL(gnttab_grant_foreign_transfer_ref); |
cl349@4693 | 46 |
cl349@4693 | 47 static grant_ref_t gnttab_free_list[NR_GRANT_ENTRIES]; |
cl349@4693 | 48 static grant_ref_t gnttab_free_head; |
cl349@4693 | 49 |
cl349@4693 | 50 static grant_entry_t *shared; |
cl349@4693 | 51 |
cl349@4693 | 52 /* |
cl349@4693 | 53 * Lock-free grant-entry allocator |
cl349@4693 | 54 */ |
cl349@4693 | 55 |
cl349@4693 | 56 static inline int |
cl349@4693 | 57 get_free_entry( |
cl349@4693 | 58 void) |
cl349@4693 | 59 { |
cl349@4693 | 60 grant_ref_t fh, nfh = gnttab_free_head; |
cl349@4693 | 61 do { if ( unlikely((fh = nfh) == NR_GRANT_ENTRIES) ) return -1; } |
cl349@4693 | 62 while ( unlikely((nfh = cmpxchg(&gnttab_free_head, fh, |
cl349@4693 | 63 gnttab_free_list[fh])) != fh) ); |
cl349@4693 | 64 return fh; |
cl349@4693 | 65 } |
cl349@4693 | 66 |
cl349@4693 | 67 static inline void |
cl349@4693 | 68 put_free_entry( |
cl349@4693 | 69 grant_ref_t ref) |
cl349@4693 | 70 { |
cl349@4693 | 71 grant_ref_t fh, nfh = gnttab_free_head; |
cl349@4693 | 72 do { gnttab_free_list[ref] = fh = nfh; wmb(); } |
cl349@4693 | 73 while ( unlikely((nfh = cmpxchg(&gnttab_free_head, fh, ref)) != fh) ); |
cl349@4693 | 74 } |
cl349@4693 | 75 |
cl349@4693 | 76 /* |
cl349@4693 | 77 * Public grant-issuing interface functions |
cl349@4693 | 78 */ |
cl349@4693 | 79 |
cl349@4693 | 80 int |
cl349@4693 | 81 gnttab_grant_foreign_access( |
cl349@4693 | 82 domid_t domid, unsigned long frame, int readonly) |
cl349@4693 | 83 { |
cl349@4693 | 84 int ref; |
cl349@4693 | 85 |
cl349@4693 | 86 if ( unlikely((ref = get_free_entry()) == -1) ) |
cl349@4693 | 87 return -ENOSPC; |
cl349@4693 | 88 |
cl349@4693 | 89 shared[ref].frame = frame; |
cl349@4693 | 90 shared[ref].domid = domid; |
cl349@4693 | 91 wmb(); |
cl349@4693 | 92 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0); |
cl349@4693 | 93 |
cl349@4693 | 94 return ref; |
cl349@4693 | 95 } |
cl349@4693 | 96 |
cl349@4693 | 97 void |
cl349@4693 | 98 gnttab_grant_foreign_access_ref( |
cl349@4693 | 99 grant_ref_t ref, domid_t domid, unsigned long frame, int readonly) |
cl349@4693 | 100 { |
cl349@4693 | 101 shared[ref].frame = frame; |
cl349@4693 | 102 shared[ref].domid = domid; |
cl349@4693 | 103 wmb(); |
cl349@4693 | 104 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0); |
cl349@4693 | 105 } |
cl349@4693 | 106 |
cl349@4693 | 107 |
cl349@4693 | 108 int |
cl349@4693 | 109 gnttab_query_foreign_access( grant_ref_t ref ) |
cl349@4693 | 110 { |
cl349@4693 | 111 u16 nflags; |
cl349@4693 | 112 |
cl349@4693 | 113 nflags = shared[ref].flags; |
cl349@4693 | 114 |
cl349@4693 | 115 return ( nflags & (GTF_reading|GTF_writing) ); |
cl349@4693 | 116 } |
cl349@4693 | 117 |
cl349@4693 | 118 void |
cl349@4693 | 119 gnttab_end_foreign_access( grant_ref_t ref, int readonly ) |
cl349@4693 | 120 { |
cl349@4693 | 121 u16 flags, nflags; |
cl349@4693 | 122 |
cl349@4693 | 123 nflags = shared[ref].flags; |
cl349@4693 | 124 do { |
cl349@4693 | 125 if ( (flags = nflags) & (GTF_reading|GTF_writing) ) |
cl349@4693 | 126 printk(KERN_ALERT "WARNING: g.e. still in use!\n"); |
cl349@4693 | 127 } |
cl349@4693 | 128 while ( (nflags = cmpxchg(&shared[ref].flags, flags, 0)) != flags ); |
cl349@4693 | 129 |
cl349@4693 | 130 put_free_entry(ref); |
cl349@4693 | 131 } |
cl349@4693 | 132 |
cl349@4693 | 133 int |
cl349@4693 | 134 gnttab_grant_foreign_transfer( |
cl349@4693 | 135 domid_t domid, unsigned long pfn ) |
cl349@4693 | 136 { |
cl349@4693 | 137 int ref; |
cl349@4693 | 138 |
cl349@4693 | 139 if ( unlikely((ref = get_free_entry()) == -1) ) |
cl349@4693 | 140 return -ENOSPC; |
cl349@4693 | 141 |
cl349@4693 | 142 shared[ref].frame = pfn; |
cl349@4693 | 143 shared[ref].domid = domid; |
cl349@4693 | 144 wmb(); |
cl349@4693 | 145 shared[ref].flags = GTF_accept_transfer; |
cl349@4693 | 146 |
cl349@4693 | 147 return ref; |
cl349@4693 | 148 } |
cl349@4693 | 149 |
cl349@4693 | 150 void |
cl349@4693 | 151 gnttab_grant_foreign_transfer_ref( |
cl349@4693 | 152 grant_ref_t ref, domid_t domid, unsigned long pfn ) |
cl349@4693 | 153 { |
cl349@4693 | 154 shared[ref].frame = pfn; |
cl349@4693 | 155 shared[ref].domid = domid; |
cl349@4693 | 156 wmb(); |
cl349@4693 | 157 shared[ref].flags = GTF_accept_transfer; |
cl349@4693 | 158 } |
cl349@4693 | 159 |
cl349@4693 | 160 unsigned long |
cl349@4693 | 161 gnttab_end_foreign_transfer( |
cl349@4693 | 162 grant_ref_t ref) |
cl349@4693 | 163 { |
cl349@4693 | 164 unsigned long frame = 0; |
cl349@4693 | 165 u16 flags; |
cl349@4693 | 166 |
cl349@4693 | 167 flags = shared[ref].flags; |
cl349@4693 | 168 ASSERT(flags == (GTF_accept_transfer | GTF_transfer_committed)); |
cl349@4693 | 169 |
cl349@4693 | 170 /* |
cl349@4693 | 171 * If a transfer is committed then wait for the frame address to appear. |
cl349@4693 | 172 * Otherwise invalidate the grant entry against future use. |
cl349@4693 | 173 */ |
cl349@4693 | 174 if ( likely(flags != GTF_accept_transfer) || |
cl349@4693 | 175 (cmpxchg(&shared[ref].flags, flags, 0) != GTF_accept_transfer) ) |
cl349@4693 | 176 while ( unlikely((frame = shared[ref].frame) == 0) ) |
cl349@4693 | 177 cpu_relax(); |
cl349@4693 | 178 |
cl349@4693 | 179 put_free_entry(ref); |
cl349@4693 | 180 |
cl349@4693 | 181 return frame; |
cl349@4693 | 182 } |
cl349@4693 | 183 |
cl349@4693 | 184 void |
cl349@4693 | 185 gnttab_free_grant_references( u16 count, grant_ref_t head ) |
cl349@4693 | 186 { |
cl349@4693 | 187 /* TODO: O(N)...? */ |
cl349@4693 | 188 grant_ref_t to_die = 0, next = head; |
cl349@4693 | 189 int i; |
cl349@4693 | 190 |
cl349@4693 | 191 for ( i = 0; i < count; i++ ) |
vh249@5272 | 192 { |
cl349@4693 | 193 to_die = next; |
cl349@4693 | 194 next = gnttab_free_list[next]; |
cl349@4693 | 195 put_free_entry( to_die ); |
vh249@5272 | 196 } |
cl349@4693 | 197 } |
cl349@4693 | 198 |
cl349@4693 | 199 int |
cl349@4693 | 200 gnttab_alloc_grant_references( u16 count, |
cl349@4693 | 201 grant_ref_t *head, |
cl349@4693 | 202 grant_ref_t *terminal ) |
cl349@4693 | 203 { |
cl349@4693 | 204 int i; |
cl349@4693 | 205 grant_ref_t h = gnttab_free_head; |
cl349@4693 | 206 |
cl349@4693 | 207 for ( i = 0; i < count; i++ ) |
cl349@4693 | 208 if ( unlikely(get_free_entry() == -1) ) |
cl349@4693 | 209 goto not_enough_refs; |
cl349@4693 | 210 |
cl349@4693 | 211 *head = h; |
cl349@4693 | 212 *terminal = gnttab_free_head; |
cl349@4693 | 213 |
cl349@4693 | 214 return 0; |
cl349@4693 | 215 |
cl349@4693 | 216 not_enough_refs: |
cl349@4693 | 217 gnttab_free_head = h; |
cl349@4693 | 218 return -ENOSPC; |
cl349@4693 | 219 } |
cl349@4693 | 220 |
cl349@4693 | 221 int |
cl349@4693 | 222 gnttab_claim_grant_reference( grant_ref_t *private_head, |
cl349@4693 | 223 grant_ref_t terminal ) |
cl349@4693 | 224 { |
cl349@4693 | 225 grant_ref_t g; |
cl349@4693 | 226 if ( unlikely((g = *private_head) == terminal) ) |
cl349@4693 | 227 return -ENOSPC; |
cl349@4693 | 228 *private_head = gnttab_free_list[g]; |
cl349@4693 | 229 return g; |
cl349@4693 | 230 } |
cl349@4693 | 231 |
cl349@4693 | 232 void |
cl349@4693 | 233 gnttab_release_grant_reference( grant_ref_t *private_head, |
cl349@4693 | 234 grant_ref_t release ) |
cl349@4693 | 235 { |
cl349@4693 | 236 gnttab_free_list[release] = *private_head; |
cl349@4693 | 237 *private_head = release; |
cl349@4693 | 238 } |
cl349@4693 | 239 |
cl349@4693 | 240 /* |
cl349@4693 | 241 * ProcFS operations |
cl349@4693 | 242 */ |
cl349@4693 | 243 |
cl349@4693 | 244 #ifdef CONFIG_PROC_FS |
cl349@4693 | 245 |
cl349@4693 | 246 static struct proc_dir_entry *grant_pde; |
cl349@4693 | 247 |
cl349@4693 | 248 static int grant_ioctl(struct inode *inode, struct file *file, |
cl349@4693 | 249 unsigned int cmd, unsigned long data) |
cl349@4693 | 250 { |
cl349@4693 | 251 int ret; |
cl349@4693 | 252 privcmd_hypercall_t hypercall; |
cl349@4693 | 253 |
cl349@4693 | 254 /* XXX Need safety checks here if using for anything other |
cl349@4693 | 255 * than debugging */ |
cl349@4693 | 256 return -ENOSYS; |
cl349@4693 | 257 |
cl349@4693 | 258 if ( cmd != IOCTL_PRIVCMD_HYPERCALL ) |
cl349@4693 | 259 return -ENOSYS; |
cl349@4693 | 260 |
cl349@4693 | 261 if ( copy_from_user(&hypercall, (void *)data, sizeof(hypercall)) ) |
cl349@4693 | 262 return -EFAULT; |
cl349@4693 | 263 |
cl349@4693 | 264 if ( hypercall.op != __HYPERVISOR_grant_table_op ) |
cl349@4693 | 265 return -ENOSYS; |
cl349@4693 | 266 |
cl349@4693 | 267 /* hypercall-invoking asm taken from privcmd.c */ |
cl349@4693 | 268 __asm__ __volatile__ ( |
cl349@4693 | 269 "pushl %%ebx; pushl %%ecx; pushl %%edx; pushl %%esi; pushl %%edi; " |
cl349@4693 | 270 "movl 4(%%eax),%%ebx ;" |
cl349@4693 | 271 "movl 8(%%eax),%%ecx ;" |
cl349@4693 | 272 "movl 12(%%eax),%%edx ;" |
cl349@4693 | 273 "movl 16(%%eax),%%esi ;" |
cl349@4693 | 274 "movl 20(%%eax),%%edi ;" |
cl349@4693 | 275 "movl (%%eax),%%eax ;" |
cl349@4693 | 276 TRAP_INSTR "; " |
cl349@4693 | 277 "popl %%edi; popl %%esi; popl %%edx; popl %%ecx; popl %%ebx" |
cl349@4693 | 278 : "=a" (ret) : "0" (&hypercall) : "memory" ); |
cl349@4693 | 279 |
cl349@4693 | 280 return ret; |
cl349@4693 | 281 } |
cl349@4693 | 282 |
cl349@4693 | 283 static struct file_operations grant_file_ops = { |
cl349@4693 | 284 ioctl: grant_ioctl, |
cl349@4693 | 285 }; |
cl349@4693 | 286 |
cl349@4693 | 287 static int grant_read(char *page, char **start, off_t off, |
cl349@4693 | 288 int count, int *eof, void *data) |
cl349@4693 | 289 { |
cl349@4693 | 290 int len; |
cl349@4693 | 291 unsigned int i; |
cl349@4693 | 292 grant_entry_t *gt; |
cl349@4693 | 293 |
cl349@4693 | 294 gt = (grant_entry_t *)shared; |
cl349@4693 | 295 len = 0; |
cl349@4693 | 296 |
cl349@4693 | 297 for ( i = 0; i < NR_GRANT_ENTRIES; i++ ) |
cl349@4693 | 298 /* TODO: safety catch here until this can handle >PAGE_SIZE output */ |
cl349@4693 | 299 if (len > (PAGE_SIZE - 200)) |
cl349@4693 | 300 { |
cl349@4693 | 301 len += sprintf( page + len, "Truncated.\n"); |
cl349@4693 | 302 break; |
cl349@4693 | 303 } |
cl349@4693 | 304 |
cl349@4693 | 305 if ( gt[i].flags ) |
cl349@4693 | 306 len += sprintf( page + len, |
cl349@4693 | 307 "Grant: ref (0x%x) flags (0x%hx) dom (0x%hx) frame (0x%x)\n", |
cl349@4693 | 308 i, |
cl349@4693 | 309 gt[i].flags, |
cl349@4693 | 310 gt[i].domid, |
cl349@4693 | 311 gt[i].frame ); |
cl349@4693 | 312 |
cl349@4693 | 313 *eof = 1; |
cl349@4693 | 314 return len; |
cl349@4693 | 315 } |
cl349@4693 | 316 |
cl349@4693 | 317 static int grant_write(struct file *file, const char __user *buffer, |
cl349@4693 | 318 unsigned long count, void *data) |
cl349@4693 | 319 { |
cl349@4693 | 320 /* TODO: implement this */ |
cl349@4693 | 321 return -ENOSYS; |
cl349@4693 | 322 } |
cl349@4693 | 323 |
cl349@4693 | 324 #endif /* CONFIG_PROC_FS */ |
cl349@4693 | 325 |
cl349@4693 | 326 int gnttab_resume(void) |
cl349@4693 | 327 { |
cl349@4693 | 328 gnttab_setup_table_t setup; |
cl349@4693 | 329 unsigned long frames[NR_GRANT_FRAMES]; |
cl349@4693 | 330 int i; |
cl349@4693 | 331 |
cl349@4693 | 332 setup.dom = DOMID_SELF; |
cl349@4693 | 333 setup.nr_frames = NR_GRANT_FRAMES; |
cl349@4693 | 334 setup.frame_list = frames; |
cl349@4693 | 335 |
cl349@4693 | 336 BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0); |
cl349@4693 | 337 BUG_ON(setup.status != 0); |
cl349@4693 | 338 |
cl349@4693 | 339 for ( i = 0; i < NR_GRANT_FRAMES; i++ ) |
cl349@4777 | 340 set_fixmap(FIX_GNTTAB_END - i, frames[i] << PAGE_SHIFT); |
cl349@4693 | 341 |
cl349@4693 | 342 return 0; |
cl349@4693 | 343 } |
cl349@4693 | 344 |
cl349@4693 | 345 int gnttab_suspend(void) |
cl349@4693 | 346 { |
cl349@4693 | 347 int i; |
cl349@4693 | 348 |
cl349@4693 | 349 for ( i = 0; i < NR_GRANT_FRAMES; i++ ) |
cl349@4693 | 350 clear_fixmap(FIX_GNTTAB_END - i); |
cl349@4693 | 351 |
cl349@4693 | 352 return 0; |
cl349@4693 | 353 } |
cl349@4693 | 354 |
cl349@4693 | 355 static int __init gnttab_init(void) |
cl349@4693 | 356 { |
cl349@4693 | 357 int i; |
cl349@4693 | 358 |
cl349@4693 | 359 BUG_ON(gnttab_resume()); |
cl349@4693 | 360 |
cl349@4693 | 361 shared = (grant_entry_t *)fix_to_virt(FIX_GNTTAB_END); |
cl349@4693 | 362 |
cl349@4693 | 363 for ( i = 0; i < NR_GRANT_ENTRIES; i++ ) |
cl349@4693 | 364 gnttab_free_list[i] = i + 1; |
cl349@4693 | 365 |
cl349@4693 | 366 #ifdef CONFIG_PROC_FS |
cl349@4693 | 367 /* |
cl349@4693 | 368 * /proc/xen/grant : used by libxc to access grant tables |
cl349@4693 | 369 */ |
cl349@4693 | 370 if ( (grant_pde = create_xen_proc_entry("grant", 0600)) == NULL ) |
cl349@4693 | 371 { |
cl349@4693 | 372 WPRINTK("Unable to create grant xen proc entry\n"); |
cl349@4693 | 373 return -1; |
cl349@4693 | 374 } |
cl349@4693 | 375 |
cl349@4693 | 376 grant_file_ops.read = grant_pde->proc_fops->read; |
cl349@4693 | 377 grant_file_ops.write = grant_pde->proc_fops->write; |
cl349@4693 | 378 |
cl349@4693 | 379 grant_pde->proc_fops = &grant_file_ops; |
cl349@4693 | 380 |
cl349@4693 | 381 grant_pde->read_proc = &grant_read; |
cl349@4693 | 382 grant_pde->write_proc = &grant_write; |
cl349@4693 | 383 #endif |
cl349@4693 | 384 |
cl349@4693 | 385 printk("Grant table initialized\n"); |
cl349@4693 | 386 return 0; |
cl349@4693 | 387 } |
cl349@4693 | 388 |
cl349@4693 | 389 __initcall(gnttab_init); |