ia64/xen-unstable

view extras/mini-os/gnttab.c @ 16488:8d406e2813c8

[Mini-OS] Make gnttab allocation/free safe

Add a semaphore to protect gnttab_list from exhaustion, and disable
callbacks during allocation/free. Fix the network frontend accordingly.

Signed-off-by: Samuel Thibault <samuel.thibault@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Nov 28 12:40:57 2007 +0000 (2007-11-28)
parents d6ca67c0645e
children d02490be30f5
line source
1 /*
2 ****************************************************************************
3 * (C) 2006 - Cambridge University
4 ****************************************************************************
5 *
6 * File: gnttab.c
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 *
10 * Date: July 2006
11 *
12 * Environment: Xen Minimal OS
13 * Description: Simple grant tables implementation. About as stupid as it's
14 * possible to be and still work.
15 *
16 ****************************************************************************
17 */
18 #include <os.h>
19 #include <mm.h>
20 #include <gnttab.h>
21 #include <semaphore.h>
23 #define NR_RESERVED_ENTRIES 8
25 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
26 #ifdef __ia64__
27 #define NR_GRANT_FRAMES 1
28 #else
29 #define NR_GRANT_FRAMES 4
30 #endif
31 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
33 static grant_entry_t *gnttab_table;
34 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
35 static __DECLARE_SEMAPHORE_GENERIC(gnttab_sem, NR_GRANT_ENTRIES);
37 static void
38 put_free_entry(grant_ref_t ref)
39 {
40 unsigned long flags;
41 local_irq_save(flags);
42 gnttab_list[ref] = gnttab_list[0];
43 gnttab_list[0] = ref;
44 local_irq_restore(flags);
45 up(&gnttab_sem);
46 }
48 static grant_ref_t
49 get_free_entry(void)
50 {
51 unsigned int ref;
52 unsigned long flags;
53 down(&gnttab_sem);
54 local_irq_save(flags);
55 ref = gnttab_list[0];
56 gnttab_list[0] = gnttab_list[ref];
57 local_irq_restore(flags);
58 return ref;
59 }
61 grant_ref_t
62 gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
63 {
64 grant_ref_t ref;
66 ref = get_free_entry();
67 gnttab_table[ref].frame = frame;
68 gnttab_table[ref].domid = domid;
69 wmb();
70 readonly *= GTF_readonly;
71 gnttab_table[ref].flags = GTF_permit_access | readonly;
73 return ref;
74 }
76 grant_ref_t
77 gnttab_grant_transfer(domid_t domid, unsigned long pfn)
78 {
79 grant_ref_t ref;
81 ref = get_free_entry();
82 gnttab_table[ref].frame = pfn;
83 gnttab_table[ref].domid = domid;
84 wmb();
85 gnttab_table[ref].flags = GTF_accept_transfer;
87 return ref;
88 }
90 int
91 gnttab_end_access(grant_ref_t ref)
92 {
93 u16 flags, nflags;
95 nflags = gnttab_table[ref].flags;
96 do {
97 if ((flags = nflags) & (GTF_reading|GTF_writing)) {
98 printk("WARNING: g.e. still in use!\n");
99 return 0;
100 }
101 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
102 flags);
104 put_free_entry(ref);
105 return 1;
106 }
108 unsigned long
109 gnttab_end_transfer(grant_ref_t ref)
110 {
111 unsigned long frame;
112 u16 flags;
114 while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
115 if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
116 printk("Release unused transfer grant.\n");
117 put_free_entry(ref);
118 return 0;
119 }
120 }
122 /* If a transfer is in progress then wait until it is completed. */
123 while (!(flags & GTF_transfer_completed)) {
124 flags = gnttab_table[ref].flags;
125 }
127 /* Read the frame number /after/ reading completion status. */
128 rmb();
129 frame = gnttab_table[ref].frame;
131 put_free_entry(ref);
133 return frame;
134 }
136 grant_ref_t
137 gnttab_alloc_and_grant(void **map)
138 {
139 unsigned long mfn;
140 grant_ref_t gref;
142 *map = (void *)alloc_page();
143 mfn = virt_to_mfn(*map);
144 gref = gnttab_grant_access(0, mfn, 0);
145 return gref;
146 }
148 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
150 const char *
151 gnttabop_error(int16_t status)
152 {
153 status = -status;
154 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
155 return "bad status";
156 else
157 return gnttabop_error_msgs[status];
158 }
160 void
161 init_gnttab(void)
162 {
163 struct gnttab_setup_table setup;
164 unsigned long frames[NR_GRANT_FRAMES];
165 int i;
167 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
168 put_free_entry(i);
170 setup.dom = DOMID_SELF;
171 setup.nr_frames = NR_GRANT_FRAMES;
172 set_xen_guest_handle(setup.frame_list, frames);
174 HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
175 gnttab_table = map_frames(frames, NR_GRANT_FRAMES);
176 printk("gnttab_table mapped at %p.\n", gnttab_table);
177 }