blob: b184652e45d7811f9b518b79b112021f1eeaa079 [file] [log] [blame]
Rusty Russellf938d2c2007-07-26 10:41:02 -07001/*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
Rusty Russell3c6b5bf2007-10-22 11:03:26 +10003 * 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. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07007#include <linux/uaccess.h>
8#include <linux/miscdevice.h>
9#include <linux/fs.h>
10#include "lg.h"
11
Rusty Russelldde79782007-07-26 10:41:03 -070012/*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a
13 * DMA buffer. This is done by writing LHREQ_GETDMA and the key to
14 * /dev/lguest. */
Jes Sorensen511801d2007-10-22 11:03:31 +100015static long user_get_dma(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070016{
17 unsigned long key, udma, irq;
18
Rusty Russelldde79782007-07-26 10:41:03 -070019 /* Fetch the key they wrote to us. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070020 if (get_user(key, input) != 0)
21 return -EFAULT;
Rusty Russelldde79782007-07-26 10:41:03 -070022 /* Look for a free Guest DMA buffer bound to that key. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070023 udma = get_dma_buffer(lg, key, &irq);
24 if (!udma)
25 return -ENOENT;
26
Rusty Russelldde79782007-07-26 10:41:03 -070027 /* We need to tell the Launcher what interrupt the Guest expects after
28 * the buffer is filled. We stash it in udma->used_len. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070029 lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq);
Rusty Russelldde79782007-07-26 10:41:03 -070030
31 /* The (guest-physical) address of the DMA buffer is returned from
32 * the write(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070033 return udma;
34}
35
Rusty Russelldde79782007-07-26 10:41:03 -070036/*L:315 To force the Guest to stop running and return to the Launcher, the
Rusty Russelld7e28ff2007-07-19 01:49:23 -070037 * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The
38 * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */
Jes Sorensen511801d2007-10-22 11:03:31 +100039static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070040{
41 unsigned long on;
42
43 /* Fetch whether they're turning break on or off.. */
44 if (get_user(on, input) != 0)
45 return -EFAULT;
46
47 if (on) {
48 lg->break_out = 1;
49 /* Pop it out (may be running on different CPU) */
50 wake_up_process(lg->tsk);
51 /* Wait for them to reset it */
52 return wait_event_interruptible(lg->break_wq, !lg->break_out);
53 } else {
54 lg->break_out = 0;
55 wake_up(&lg->break_wq);
56 return 0;
57 }
58}
59
Rusty Russelldde79782007-07-26 10:41:03 -070060/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
61 * number to /dev/lguest. */
Jes Sorensen511801d2007-10-22 11:03:31 +100062static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070063{
Jes Sorensen511801d2007-10-22 11:03:31 +100064 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070065
66 if (get_user(irq, input) != 0)
67 return -EFAULT;
68 if (irq >= LGUEST_IRQS)
69 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -070070 /* Next time the Guest runs, the core code will see if it can deliver
71 * this interrupt. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072 set_bit(irq, lg->irqs_pending);
73 return 0;
74}
75
Rusty Russelldde79782007-07-26 10:41:03 -070076/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
77 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070078static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
79{
80 struct lguest *lg = file->private_data;
81
Rusty Russelldde79782007-07-26 10:41:03 -070082 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070083 if (!lg)
84 return -EINVAL;
85
86 /* If you're not the task which owns the guest, go away. */
87 if (current != lg->tsk)
88 return -EPERM;
89
Rusty Russelldde79782007-07-26 10:41:03 -070090 /* If the guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070091 if (lg->dead) {
92 size_t len;
93
Rusty Russelldde79782007-07-26 10:41:03 -070094 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070095 if (IS_ERR(lg->dead))
96 return PTR_ERR(lg->dead);
97
Rusty Russelldde79782007-07-26 10:41:03 -070098 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070099 len = min(size, strlen(lg->dead)+1);
100 if (copy_to_user(user, lg->dead, len) != 0)
101 return -EFAULT;
102 return len;
103 }
104
Rusty Russelldde79782007-07-26 10:41:03 -0700105 /* If we returned from read() last time because the Guest sent DMA,
106 * clear the flag. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700107 if (lg->dma_is_pending)
108 lg->dma_is_pending = 0;
109
Rusty Russelldde79782007-07-26 10:41:03 -0700110 /* Run the Guest until something interesting happens. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700111 return run_guest(lg, (unsigned long __user *)user);
112}
113
Jes Sorensen511801d2007-10-22 11:03:31 +1000114/*L:020 The initialization write supplies 5 pointer sized (32 or 64 bit)
115 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700116 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000117 * base: The start of the Guest-physical memory inside the Launcher memory.
118 *
Rusty Russelldde79782007-07-26 10:41:03 -0700119 * pfnlimit: The highest (Guest-physical) page number the Guest should be
120 * allowed to access. The Launcher has to live in Guest memory, so it sets
121 * this to ensure the Guest can't reach it.
122 *
123 * pgdir: The (Guest-physical) address of the top of the initial Guest
124 * pagetables (which are set up by the Launcher).
125 *
126 * start: The first instruction to execute ("eip" in x86-speak).
127 *
128 * page_offset: The PAGE_OFFSET constant in the Guest kernel. We should
129 * probably wean the code off this, but it's a very useful constant! Any
130 * address above this is within the Guest kernel, and any kernel address can
131 * quickly converted from physical to virtual by adding PAGE_OFFSET. It's
132 * 0xC0000000 (3G) by default, but it's configurable at kernel build time.
133 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000134static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700135{
Rusty Russelldde79782007-07-26 10:41:03 -0700136 /* "struct lguest" contains everything we (the Host) know about a
137 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700138 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000139 int err;
Jes Sorensen511801d2007-10-22 11:03:31 +1000140 unsigned long args[5];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700141
Rusty Russell48245cc2007-10-22 11:03:27 +1000142 /* We grab the Big Lguest lock, which protects against multiple
143 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700144 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700145 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700146 if (file->private_data) {
147 err = -EBUSY;
148 goto unlock;
149 }
150
151 if (copy_from_user(args, input, sizeof(args)) != 0) {
152 err = -EFAULT;
153 goto unlock;
154 }
155
Rusty Russell48245cc2007-10-22 11:03:27 +1000156 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
157 if (!lg) {
158 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700159 goto unlock;
160 }
Rusty Russelldde79782007-07-26 10:41:03 -0700161
162 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000163 lg->mem_base = (void __user *)(long)args[0];
164 lg->pfn_limit = args[1];
165 lg->page_offset = args[4];
Rusty Russelldde79782007-07-26 10:41:03 -0700166
167 /* We need a complete page for the Guest registers: they are accessible
168 * to the Guest and we can only grant it access to whole pages. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700169 lg->regs_page = get_zeroed_page(GFP_KERNEL);
170 if (!lg->regs_page) {
171 err = -ENOMEM;
172 goto release_guest;
173 }
Rusty Russelldde79782007-07-26 10:41:03 -0700174 /* We actually put the registers at the bottom of the page. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
176
Rusty Russelldde79782007-07-26 10:41:03 -0700177 /* Initialize the Guest's shadow page tables, using the toplevel
178 * address the Launcher gave us. This allocates memory, so can
179 * fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000180 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700181 if (err)
182 goto free_regs;
183
Rusty Russelldde79782007-07-26 10:41:03 -0700184 /* Now we initialize the Guest's registers, handing it the start
185 * address. */
Jes Sorensend612cde2007-10-22 11:03:32 +1000186 lguest_arch_setup_regs(lg, args[3]);
Rusty Russelldde79782007-07-26 10:41:03 -0700187
188 /* The timer for lguest's clock needs initialization. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700189 init_clockdev(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700190
191 /* We keep a pointer to the Launcher task (ie. current task) for when
192 * other Guests want to wake this one (inter-Guest I/O). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700193 lg->tsk = current;
Rusty Russelldde79782007-07-26 10:41:03 -0700194 /* We need to keep a pointer to the Launcher's memory map, because if
195 * the Launcher dies we need to clean it up. If we don't keep a
196 * reference, it is destroyed before close() is called. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700197 lg->mm = get_task_mm(lg->tsk);
Rusty Russelldde79782007-07-26 10:41:03 -0700198
199 /* Initialize the queue for the waker to wait on */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700200 init_waitqueue_head(&lg->break_wq);
Rusty Russelldde79782007-07-26 10:41:03 -0700201
202 /* We remember which CPU's pages this Guest used last, for optimization
203 * when the same Guest runs on the same CPU twice. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700204 lg->last_pages = NULL;
Rusty Russelldde79782007-07-26 10:41:03 -0700205
206 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700207 file->private_data = lg;
208
209 mutex_unlock(&lguest_lock);
210
Rusty Russelldde79782007-07-26 10:41:03 -0700211 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700212 return sizeof(args);
213
214free_regs:
215 free_page(lg->regs_page);
216release_guest:
217 memset(lg, 0, sizeof(*lg));
218unlock:
219 mutex_unlock(&lguest_lock);
220 return err;
221}
222
Rusty Russelldde79782007-07-26 10:41:03 -0700223/*L:010 The first operation the Launcher does must be a write. All writes
224 * start with a 32 bit number: for the first write this must be
225 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
226 * writes of other values to get DMA buffers and send interrupts. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000227static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228 size_t size, loff_t *off)
229{
Rusty Russelldde79782007-07-26 10:41:03 -0700230 /* Once the guest is initialized, we hold the "struct lguest" in the
231 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700232 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000233 const unsigned long __user *input = (const unsigned long __user *)in;
234 unsigned long req;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235
236 if (get_user(req, input) != 0)
237 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000238 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239
Rusty Russelldde79782007-07-26 10:41:03 -0700240 /* If you haven't initialized, you must do that first. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700241 if (req != LHREQ_INITIALIZE && !lg)
242 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -0700243
244 /* Once the Guest is dead, all you can do is read() why it died. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700245 if (lg && lg->dead)
246 return -ENOENT;
247
248 /* If you're not the task which owns the Guest, you can only break */
249 if (lg && current != lg->tsk && req != LHREQ_BREAK)
250 return -EPERM;
251
252 switch (req) {
253 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000254 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700255 case LHREQ_GETDMA:
Jes Sorensen511801d2007-10-22 11:03:31 +1000256 return user_get_dma(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700257 case LHREQ_IRQ:
Jes Sorensen511801d2007-10-22 11:03:31 +1000258 return user_send_irq(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700259 case LHREQ_BREAK:
Jes Sorensen511801d2007-10-22 11:03:31 +1000260 return break_guest_out(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700261 default:
262 return -EINVAL;
263 }
264}
265
Rusty Russelldde79782007-07-26 10:41:03 -0700266/*L:060 The final piece of interface code is the close() routine. It reverses
267 * everything done in initialize(). This is usually called because the
268 * Launcher exited.
269 *
270 * Note that the close routine returns 0 or a negative error number: it can't
271 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
272 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700273static int close(struct inode *inode, struct file *file)
274{
275 struct lguest *lg = file->private_data;
276
Rusty Russelldde79782007-07-26 10:41:03 -0700277 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700278 if (!lg)
279 return 0;
280
Rusty Russelldde79782007-07-26 10:41:03 -0700281 /* We need the big lock, to protect from inter-guest I/O and other
282 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700283 mutex_lock(&lguest_lock);
284 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
285 hrtimer_cancel(&lg->hrt);
Rusty Russelldde79782007-07-26 10:41:03 -0700286 /* Free any DMA buffers the Guest had bound. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700287 release_all_dma(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700288 /* Free up the shadow page tables for the Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700289 free_guest_pagetable(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700290 /* Now all the memory cleanups are done, it's safe to release the
291 * Launcher's memory management structure. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 mmput(lg->mm);
Rusty Russelldde79782007-07-26 10:41:03 -0700293 /* If lg->dead doesn't contain an error code it will be NULL or a
294 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700295 if (!IS_ERR(lg->dead))
296 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700297 /* We can free up the register page we allocated. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700298 free_page(lg->regs_page);
Rusty Russelldde79782007-07-26 10:41:03 -0700299 /* We clear the entire structure, which also marks it as free for the
300 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700301 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700302 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700303 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700304
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700305 return 0;
306}
307
Rusty Russelldde79782007-07-26 10:41:03 -0700308/*L:000
309 * Welcome to our journey through the Launcher!
310 *
311 * The Launcher is the Host userspace program which sets up, runs and services
312 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
313 * doing things are inaccurate: the Launcher does all the device handling for
314 * the Guest. The Guest can't tell what's done by the the Launcher and what by
315 * the Host.
316 *
317 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
318 * shall see more of that later.
319 *
320 * We begin our understanding with the Host kernel interface which the Launcher
321 * uses: reading and writing a character device called /dev/lguest. All the
322 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700323static struct file_operations lguest_fops = {
324 .owner = THIS_MODULE,
325 .release = close,
326 .write = write,
327 .read = read,
328};
Rusty Russelldde79782007-07-26 10:41:03 -0700329
330/* This is a textbook example of a "misc" character device. Populate a "struct
331 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700332static struct miscdevice lguest_dev = {
333 .minor = MISC_DYNAMIC_MINOR,
334 .name = "lguest",
335 .fops = &lguest_fops,
336};
337
338int __init lguest_device_init(void)
339{
340 return misc_register(&lguest_dev);
341}
342
343void __exit lguest_device_remove(void)
344{
345 misc_deregister(&lguest_dev);
346}