]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/lguest/lguest_user.c
Introduce guest mem offset, static link example launcher
[linux-2.6.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 DMA out to the Launcher.  Writes are also used to get a
6  * DMA buffer registered by the Guest and to send the Guest an interrupt. :*/
7 #include <linux/uaccess.h>
8 #include <linux/miscdevice.h>
9 #include <linux/fs.h>
10 #include "lg.h"
11
12 /*L:030 setup_regs() doesn't really belong in this file, but it gives us an
13  * early glimpse deeper into the Host so it's worth having here.
14  *
15  * Most of the Guest's registers are left alone: we used get_zeroed_page() to
16  * allocate the structure, so they will be 0. */
17 static void setup_regs(struct lguest_regs *regs, unsigned long start)
18 {
19         /* There are four "segment" registers which the Guest needs to boot:
20          * The "code segment" register (cs) refers to the kernel code segment
21          * __KERNEL_CS, and the "data", "extra" and "stack" segment registers
22          * refer to the kernel data segment __KERNEL_DS.
23          *
24          * The privilege level is packed into the lower bits.  The Guest runs
25          * at privilege level 1 (GUEST_PL).*/
26         regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL;
27         regs->cs = __KERNEL_CS|GUEST_PL;
28
29         /* The "eflags" register contains miscellaneous flags.  Bit 1 (0x002)
30          * is supposed to always be "1".  Bit 9 (0x200) controls whether
31          * interrupts are enabled.  We always leave interrupts enabled while
32          * running the Guest. */
33         regs->eflags = 0x202;
34
35         /* The "Extended Instruction Pointer" register says where the Guest is
36          * running. */
37         regs->eip = start;
38
39         /* %esi points to our boot information, at physical address 0, so don't
40          * touch it. */
41 }
42
43 /*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a
44  * DMA buffer.  This is done by writing LHREQ_GETDMA and the key to
45  * /dev/lguest. */
46 static long user_get_dma(struct lguest *lg, const u32 __user *input)
47 {
48         unsigned long key, udma, irq;
49
50         /* Fetch the key they wrote to us. */
51         if (get_user(key, input) != 0)
52                 return -EFAULT;
53         /* Look for a free Guest DMA buffer bound to that key. */
54         udma = get_dma_buffer(lg, key, &irq);
55         if (!udma)
56                 return -ENOENT;
57
58         /* We need to tell the Launcher what interrupt the Guest expects after
59          * the buffer is filled.  We stash it in udma->used_len. */
60         lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq);
61
62         /* The (guest-physical) address of the DMA buffer is returned from
63          * the write(). */
64         return udma;
65 }
66
67 /*L:315 To force the Guest to stop running and return to the Launcher, the
68  * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest.  The
69  * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */
70 static int break_guest_out(struct lguest *lg, const u32 __user *input)
71 {
72         unsigned long on;
73
74         /* Fetch whether they're turning break on or off.. */
75         if (get_user(on, input) != 0)
76                 return -EFAULT;
77
78         if (on) {
79                 lg->break_out = 1;
80                 /* Pop it out (may be running on different CPU) */
81                 wake_up_process(lg->tsk);
82                 /* Wait for them to reset it */
83                 return wait_event_interruptible(lg->break_wq, !lg->break_out);
84         } else {
85                 lg->break_out = 0;
86                 wake_up(&lg->break_wq);
87                 return 0;
88         }
89 }
90
91 /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
92  * number to /dev/lguest. */
93 static int user_send_irq(struct lguest *lg, const u32 __user *input)
94 {
95         u32 irq;
96
97         if (get_user(irq, input) != 0)
98                 return -EFAULT;
99         if (irq >= LGUEST_IRQS)
100                 return -EINVAL;
101         /* Next time the Guest runs, the core code will see if it can deliver
102          * this interrupt. */
103         set_bit(irq, lg->irqs_pending);
104         return 0;
105 }
106
107 /*L:040 Once our Guest is initialized, the Launcher makes it run by reading
108  * from /dev/lguest. */
109 static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
110 {
111         struct lguest *lg = file->private_data;
112
113         /* You must write LHREQ_INITIALIZE first! */
114         if (!lg)
115                 return -EINVAL;
116
117         /* If you're not the task which owns the guest, go away. */
118         if (current != lg->tsk)
119                 return -EPERM;
120
121         /* If the guest is already dead, we indicate why */
122         if (lg->dead) {
123                 size_t len;
124
125                 /* lg->dead either contains an error code, or a string. */
126                 if (IS_ERR(lg->dead))
127                         return PTR_ERR(lg->dead);
128
129                 /* We can only return as much as the buffer they read with. */
130                 len = min(size, strlen(lg->dead)+1);
131                 if (copy_to_user(user, lg->dead, len) != 0)
132                         return -EFAULT;
133                 return len;
134         }
135
136         /* If we returned from read() last time because the Guest sent DMA,
137          * clear the flag. */
138         if (lg->dma_is_pending)
139                 lg->dma_is_pending = 0;
140
141         /* Run the Guest until something interesting happens. */
142         return run_guest(lg, (unsigned long __user *)user);
143 }
144
145 /*L:020 The initialization write supplies 5 32-bit values (in addition to the
146  * 32-bit LHREQ_INITIALIZE value).  These are:
147  *
148  * base: The start of the Guest-physical memory inside the Launcher memory.
149  *
150  * pfnlimit: The highest (Guest-physical) page number the Guest should be
151  * allowed to access.  The Launcher has to live in Guest memory, so it sets
152  * this to ensure the Guest can't reach it.
153  *
154  * pgdir: The (Guest-physical) address of the top of the initial Guest
155  * pagetables (which are set up by the Launcher).
156  *
157  * start: The first instruction to execute ("eip" in x86-speak).
158  *
159  * page_offset: The PAGE_OFFSET constant in the Guest kernel.  We should
160  * probably wean the code off this, but it's a very useful constant!  Any
161  * address above this is within the Guest kernel, and any kernel address can
162  * quickly converted from physical to virtual by adding PAGE_OFFSET.  It's
163  * 0xC0000000 (3G) by default, but it's configurable at kernel build time.
164  */
165 static int initialize(struct file *file, const u32 __user *input)
166 {
167         /* "struct lguest" contains everything we (the Host) know about a
168          * Guest. */
169         struct lguest *lg;
170         int err, i;
171         u32 args[5];
172
173         /* We grab the Big Lguest lock, which protects the global array
174          * "lguests" and multiple simultaneous initializations. */
175         mutex_lock(&lguest_lock);
176         /* You can't initialize twice!  Close the device and start again... */
177         if (file->private_data) {
178                 err = -EBUSY;
179                 goto unlock;
180         }
181
182         if (copy_from_user(args, input, sizeof(args)) != 0) {
183                 err = -EFAULT;
184                 goto unlock;
185         }
186
187         /* Find an unused guest. */
188         i = find_free_guest();
189         if (i < 0) {
190                 err = -ENOSPC;
191                 goto unlock;
192         }
193         /* OK, we have an index into the "lguest" array: "lg" is a convenient
194          * pointer. */
195         lg = &lguests[i];
196
197         /* Populate the easy fields of our "struct lguest" */
198         lg->guestid = i;
199         lg->mem_base = (void __user *)(long)args[0];
200         lg->pfn_limit = args[1];
201         lg->page_offset = args[4];
202
203         /* We need a complete page for the Guest registers: they are accessible
204          * to the Guest and we can only grant it access to whole pages. */
205         lg->regs_page = get_zeroed_page(GFP_KERNEL);
206         if (!lg->regs_page) {
207                 err = -ENOMEM;
208                 goto release_guest;
209         }
210         /* We actually put the registers at the bottom of the page. */
211         lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
212
213         /* Initialize the Guest's shadow page tables, using the toplevel
214          * address the Launcher gave us.  This allocates memory, so can
215          * fail. */
216         err = init_guest_pagetable(lg, args[2]);
217         if (err)
218                 goto free_regs;
219
220         /* Now we initialize the Guest's registers, handing it the start
221          * address. */
222         setup_regs(lg->regs, args[3]);
223
224         /* There are a couple of GDT entries the Guest expects when first
225          * booting. */
226         setup_guest_gdt(lg);
227
228         /* The timer for lguest's clock needs initialization. */
229         init_clockdev(lg);
230
231         /* We keep a pointer to the Launcher task (ie. current task) for when
232          * other Guests want to wake this one (inter-Guest I/O). */
233         lg->tsk = current;
234         /* We need to keep a pointer to the Launcher's memory map, because if
235          * the Launcher dies we need to clean it up.  If we don't keep a
236          * reference, it is destroyed before close() is called. */
237         lg->mm = get_task_mm(lg->tsk);
238
239         /* Initialize the queue for the waker to wait on */
240         init_waitqueue_head(&lg->break_wq);
241
242         /* We remember which CPU's pages this Guest used last, for optimization
243          * when the same Guest runs on the same CPU twice. */
244         lg->last_pages = NULL;
245
246         /* We keep our "struct lguest" in the file's private_data. */
247         file->private_data = lg;
248
249         mutex_unlock(&lguest_lock);
250
251         /* And because this is a write() call, we return the length used. */
252         return sizeof(args);
253
254 free_regs:
255         free_page(lg->regs_page);
256 release_guest:
257         memset(lg, 0, sizeof(*lg));
258 unlock:
259         mutex_unlock(&lguest_lock);
260         return err;
261 }
262
263 /*L:010 The first operation the Launcher does must be a write.  All writes
264  * start with a 32 bit number: for the first write this must be
265  * LHREQ_INITIALIZE to set up the Guest.  After that the Launcher can use
266  * writes of other values to get DMA buffers and send interrupts. */
267 static ssize_t write(struct file *file, const char __user *input,
268                      size_t size, loff_t *off)
269 {
270         /* Once the guest is initialized, we hold the "struct lguest" in the
271          * file private data. */
272         struct lguest *lg = file->private_data;
273         u32 req;
274
275         if (get_user(req, input) != 0)
276                 return -EFAULT;
277         input += sizeof(req);
278
279         /* If you haven't initialized, you must do that first. */
280         if (req != LHREQ_INITIALIZE && !lg)
281                 return -EINVAL;
282
283         /* Once the Guest is dead, all you can do is read() why it died. */
284         if (lg && lg->dead)
285                 return -ENOENT;
286
287         /* If you're not the task which owns the Guest, you can only break */
288         if (lg && current != lg->tsk && req != LHREQ_BREAK)
289                 return -EPERM;
290
291         switch (req) {
292         case LHREQ_INITIALIZE:
293                 return initialize(file, (const u32 __user *)input);
294         case LHREQ_GETDMA:
295                 return user_get_dma(lg, (const u32 __user *)input);
296         case LHREQ_IRQ:
297                 return user_send_irq(lg, (const u32 __user *)input);
298         case LHREQ_BREAK:
299                 return break_guest_out(lg, (const u32 __user *)input);
300         default:
301                 return -EINVAL;
302         }
303 }
304
305 /*L:060 The final piece of interface code is the close() routine.  It reverses
306  * everything done in initialize().  This is usually called because the
307  * Launcher exited.
308  *
309  * Note that the close routine returns 0 or a negative error number: it can't
310  * really fail, but it can whine.  I blame Sun for this wart, and K&R C for
311  * letting them do it. :*/
312 static int close(struct inode *inode, struct file *file)
313 {
314         struct lguest *lg = file->private_data;
315
316         /* If we never successfully initialized, there's nothing to clean up */
317         if (!lg)
318                 return 0;
319
320         /* We need the big lock, to protect from inter-guest I/O and other
321          * Launchers initializing guests. */
322         mutex_lock(&lguest_lock);
323         /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
324         hrtimer_cancel(&lg->hrt);
325         /* Free any DMA buffers the Guest had bound. */
326         release_all_dma(lg);
327         /* Free up the shadow page tables for the Guest. */
328         free_guest_pagetable(lg);
329         /* Now all the memory cleanups are done, it's safe to release the
330          * Launcher's memory management structure. */
331         mmput(lg->mm);
332         /* If lg->dead doesn't contain an error code it will be NULL or a
333          * kmalloc()ed string, either of which is ok to hand to kfree(). */
334         if (!IS_ERR(lg->dead))
335                 kfree(lg->dead);
336         /* We can free up the register page we allocated. */
337         free_page(lg->regs_page);
338         /* We clear the entire structure, which also marks it as free for the
339          * next user. */
340         memset(lg, 0, sizeof(*lg));
341         /* Release lock and exit. */
342         mutex_unlock(&lguest_lock);
343
344         return 0;
345 }
346
347 /*L:000
348  * Welcome to our journey through the Launcher!
349  *
350  * The Launcher is the Host userspace program which sets up, runs and services
351  * the Guest.  In fact, many comments in the Drivers which refer to "the Host"
352  * doing things are inaccurate: the Launcher does all the device handling for
353  * the Guest.  The Guest can't tell what's done by the the Launcher and what by
354  * the Host.
355  *
356  * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
357  * shall see more of that later.
358  *
359  * We begin our understanding with the Host kernel interface which the Launcher
360  * uses: reading and writing a character device called /dev/lguest.  All the
361  * work happens in the read(), write() and close() routines: */
362 static struct file_operations lguest_fops = {
363         .owner   = THIS_MODULE,
364         .release = close,
365         .write   = write,
366         .read    = read,
367 };
368
369 /* This is a textbook example of a "misc" character device.  Populate a "struct
370  * miscdevice" and register it with misc_register(). */
371 static struct miscdevice lguest_dev = {
372         .minor  = MISC_DYNAMIC_MINOR,
373         .name   = "lguest",
374         .fops   = &lguest_fops,
375 };
376
377 int __init lguest_device_init(void)
378 {
379         return misc_register(&lguest_dev);
380 }
381
382 void __exit lguest_device_remove(void)
383 {
384         misc_deregister(&lguest_dev);
385 }