Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[deliverable/linux.git] / drivers / lguest / lguest_user.c
1 /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher
2 * controls and communicates with the Guest. For example, the first write will
3 * tell us the Guest's memory layout, pagetable, entry point and kernel address
4 * offset. A read will run the Guest until something happens, such as a signal
5 * or the Guest doing a NOTIFY out to the Launcher. :*/
6 #include <linux/uaccess.h>
7 #include <linux/miscdevice.h>
8 #include <linux/fs.h>
9 #include "lg.h"
10
11 /*L:315 To force the Guest to stop running and return to the Launcher, the
12 * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The
13 * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */
14 static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
15 {
16 unsigned long on;
17
18 /* Fetch whether they're turning break on or off.. */
19 if (get_user(on, input) != 0)
20 return -EFAULT;
21
22 if (on) {
23 lg->break_out = 1;
24 /* Pop it out (may be running on different CPU) */
25 wake_up_process(lg->tsk);
26 /* Wait for them to reset it */
27 return wait_event_interruptible(lg->break_wq, !lg->break_out);
28 } else {
29 lg->break_out = 0;
30 wake_up(&lg->break_wq);
31 return 0;
32 }
33 }
34
35 /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
36 * number to /dev/lguest. */
37 static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
38 {
39 unsigned long irq;
40
41 if (get_user(irq, input) != 0)
42 return -EFAULT;
43 if (irq >= LGUEST_IRQS)
44 return -EINVAL;
45 /* Next time the Guest runs, the core code will see if it can deliver
46 * this interrupt. */
47 set_bit(irq, lg->irqs_pending);
48 return 0;
49 }
50
51 /*L:040 Once our Guest is initialized, the Launcher makes it run by reading
52 * from /dev/lguest. */
53 static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
54 {
55 struct lguest *lg = file->private_data;
56
57 /* You must write LHREQ_INITIALIZE first! */
58 if (!lg)
59 return -EINVAL;
60
61 /* If you're not the task which owns the guest, go away. */
62 if (current != lg->tsk)
63 return -EPERM;
64
65 /* If the guest is already dead, we indicate why */
66 if (lg->dead) {
67 size_t len;
68
69 /* lg->dead either contains an error code, or a string. */
70 if (IS_ERR(lg->dead))
71 return PTR_ERR(lg->dead);
72
73 /* We can only return as much as the buffer they read with. */
74 len = min(size, strlen(lg->dead)+1);
75 if (copy_to_user(user, lg->dead, len) != 0)
76 return -EFAULT;
77 return len;
78 }
79
80 /* If we returned from read() last time because the Guest notified,
81 * clear the flag. */
82 if (lg->pending_notify)
83 lg->pending_notify = 0;
84
85 /* Run the Guest until something interesting happens. */
86 return run_guest(lg, (unsigned long __user *)user);
87 }
88
89 /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
90 * values (in addition to the LHREQ_INITIALIZE value). These are:
91 *
92 * base: The start of the Guest-physical memory inside the Launcher memory.
93 *
94 * pfnlimit: The highest (Guest-physical) page number the Guest should be
95 * allowed to access. The Launcher has to live in Guest memory, so it sets
96 * this to ensure the Guest can't reach it.
97 *
98 * pgdir: The (Guest-physical) address of the top of the initial Guest
99 * pagetables (which are set up by the Launcher).
100 *
101 * start: The first instruction to execute ("eip" in x86-speak).
102 */
103 static int initialize(struct file *file, const unsigned long __user *input)
104 {
105 /* "struct lguest" contains everything we (the Host) know about a
106 * Guest. */
107 struct lguest *lg;
108 int err;
109 unsigned long args[4];
110
111 /* We grab the Big Lguest lock, which protects against multiple
112 * simultaneous initializations. */
113 mutex_lock(&lguest_lock);
114 /* You can't initialize twice! Close the device and start again... */
115 if (file->private_data) {
116 err = -EBUSY;
117 goto unlock;
118 }
119
120 if (copy_from_user(args, input, sizeof(args)) != 0) {
121 err = -EFAULT;
122 goto unlock;
123 }
124
125 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
126 if (!lg) {
127 err = -ENOMEM;
128 goto unlock;
129 }
130
131 /* Populate the easy fields of our "struct lguest" */
132 lg->mem_base = (void __user *)(long)args[0];
133 lg->pfn_limit = args[1];
134
135 /* We need a complete page for the Guest registers: they are accessible
136 * to the Guest and we can only grant it access to whole pages. */
137 lg->regs_page = get_zeroed_page(GFP_KERNEL);
138 if (!lg->regs_page) {
139 err = -ENOMEM;
140 goto release_guest;
141 }
142 /* We actually put the registers at the bottom of the page. */
143 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
144
145 /* Initialize the Guest's shadow page tables, using the toplevel
146 * address the Launcher gave us. This allocates memory, so can
147 * fail. */
148 err = init_guest_pagetable(lg, args[2]);
149 if (err)
150 goto free_regs;
151
152 /* Now we initialize the Guest's registers, handing it the start
153 * address. */
154 lguest_arch_setup_regs(lg, args[3]);
155
156 /* The timer for lguest's clock needs initialization. */
157 init_clockdev(lg);
158
159 /* We keep a pointer to the Launcher task (ie. current task) for when
160 * other Guests want to wake this one (inter-Guest I/O). */
161 lg->tsk = current;
162 /* We need to keep a pointer to the Launcher's memory map, because if
163 * the Launcher dies we need to clean it up. If we don't keep a
164 * reference, it is destroyed before close() is called. */
165 lg->mm = get_task_mm(lg->tsk);
166
167 /* Initialize the queue for the waker to wait on */
168 init_waitqueue_head(&lg->break_wq);
169
170 /* We remember which CPU's pages this Guest used last, for optimization
171 * when the same Guest runs on the same CPU twice. */
172 lg->last_pages = NULL;
173
174 /* We keep our "struct lguest" in the file's private_data. */
175 file->private_data = lg;
176
177 mutex_unlock(&lguest_lock);
178
179 /* And because this is a write() call, we return the length used. */
180 return sizeof(args);
181
182 free_regs:
183 free_page(lg->regs_page);
184 release_guest:
185 memset(lg, 0, sizeof(*lg));
186 unlock:
187 mutex_unlock(&lguest_lock);
188 return err;
189 }
190
191 /*L:010 The first operation the Launcher does must be a write. All writes
192 * start with a 32 bit number: for the first write this must be
193 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
194 * writes of other values to send interrupts. */
195 static ssize_t write(struct file *file, const char __user *in,
196 size_t size, loff_t *off)
197 {
198 /* Once the guest is initialized, we hold the "struct lguest" in the
199 * file private data. */
200 struct lguest *lg = file->private_data;
201 const unsigned long __user *input = (const unsigned long __user *)in;
202 unsigned long req;
203
204 if (get_user(req, input) != 0)
205 return -EFAULT;
206 input++;
207
208 /* If you haven't initialized, you must do that first. */
209 if (req != LHREQ_INITIALIZE && !lg)
210 return -EINVAL;
211
212 /* Once the Guest is dead, all you can do is read() why it died. */
213 if (lg && lg->dead)
214 return -ENOENT;
215
216 /* If you're not the task which owns the Guest, you can only break */
217 if (lg && current != lg->tsk && req != LHREQ_BREAK)
218 return -EPERM;
219
220 switch (req) {
221 case LHREQ_INITIALIZE:
222 return initialize(file, input);
223 case LHREQ_IRQ:
224 return user_send_irq(lg, input);
225 case LHREQ_BREAK:
226 return break_guest_out(lg, input);
227 default:
228 return -EINVAL;
229 }
230 }
231
232 /*L:060 The final piece of interface code is the close() routine. It reverses
233 * everything done in initialize(). This is usually called because the
234 * Launcher exited.
235 *
236 * Note that the close routine returns 0 or a negative error number: it can't
237 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
238 * letting them do it. :*/
239 static int close(struct inode *inode, struct file *file)
240 {
241 struct lguest *lg = file->private_data;
242
243 /* If we never successfully initialized, there's nothing to clean up */
244 if (!lg)
245 return 0;
246
247 /* We need the big lock, to protect from inter-guest I/O and other
248 * Launchers initializing guests. */
249 mutex_lock(&lguest_lock);
250 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
251 hrtimer_cancel(&lg->hrt);
252 /* Free up the shadow page tables for the Guest. */
253 free_guest_pagetable(lg);
254 /* Now all the memory cleanups are done, it's safe to release the
255 * Launcher's memory management structure. */
256 mmput(lg->mm);
257 /* If lg->dead doesn't contain an error code it will be NULL or a
258 * kmalloc()ed string, either of which is ok to hand to kfree(). */
259 if (!IS_ERR(lg->dead))
260 kfree(lg->dead);
261 /* We can free up the register page we allocated. */
262 free_page(lg->regs_page);
263 /* We clear the entire structure, which also marks it as free for the
264 * next user. */
265 memset(lg, 0, sizeof(*lg));
266 /* Release lock and exit. */
267 mutex_unlock(&lguest_lock);
268
269 return 0;
270 }
271
272 /*L:000
273 * Welcome to our journey through the Launcher!
274 *
275 * The Launcher is the Host userspace program which sets up, runs and services
276 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
277 * doing things are inaccurate: the Launcher does all the device handling for
278 * the Guest. The Guest can't tell what's done by the the Launcher and what by
279 * the Host.
280 *
281 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
282 * shall see more of that later.
283 *
284 * We begin our understanding with the Host kernel interface which the Launcher
285 * uses: reading and writing a character device called /dev/lguest. All the
286 * work happens in the read(), write() and close() routines: */
287 static struct file_operations lguest_fops = {
288 .owner = THIS_MODULE,
289 .release = close,
290 .write = write,
291 .read = read,
292 };
293
294 /* This is a textbook example of a "misc" character device. Populate a "struct
295 * miscdevice" and register it with misc_register(). */
296 static struct miscdevice lguest_dev = {
297 .minor = MISC_DYNAMIC_MINOR,
298 .name = "lguest",
299 .fops = &lguest_fops,
300 };
301
302 int __init lguest_device_init(void)
303 {
304 return misc_register(&lguest_dev);
305 }
306
307 void __exit lguest_device_remove(void)
308 {
309 misc_deregister(&lguest_dev);
310 }
This page took 0.037399 seconds and 6 git commands to generate.