blob: d4ac5f84642760a6e19c560f807f8032aa7b2fb1 [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: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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070017static void setup_regs(struct lguest_regs *regs, unsigned long start)
18{
Rusty Russelldde79782007-07-26 10:41:03 -070019 /* 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).*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -070026 regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL;
27 regs->cs = __KERNEL_CS|GUEST_PL;
Rusty Russelldde79782007-07-26 10:41:03 -070028
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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070037 regs->eip = start;
Rusty Russelldde79782007-07-26 10:41:03 -070038
39 /* %esi points to our boot information, at physical address 0, so don't
40 * touch it. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070041}
42
Rusty Russelldde79782007-07-26 10:41:03 -070043/*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. */
Jes Sorensen511801d2007-10-22 11:03:31 +100046static long user_get_dma(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070047{
48 unsigned long key, udma, irq;
49
Rusty Russelldde79782007-07-26 10:41:03 -070050 /* Fetch the key they wrote to us. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070051 if (get_user(key, input) != 0)
52 return -EFAULT;
Rusty Russelldde79782007-07-26 10:41:03 -070053 /* Look for a free Guest DMA buffer bound to that key. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070054 udma = get_dma_buffer(lg, key, &irq);
55 if (!udma)
56 return -ENOENT;
57
Rusty Russelldde79782007-07-26 10:41:03 -070058 /* We need to tell the Launcher what interrupt the Guest expects after
59 * the buffer is filled. We stash it in udma->used_len. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070060 lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq);
Rusty Russelldde79782007-07-26 10:41:03 -070061
62 /* The (guest-physical) address of the DMA buffer is returned from
63 * the write(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070064 return udma;
65}
66
Rusty Russelldde79782007-07-26 10:41:03 -070067/*L:315 To force the Guest to stop running and return to the Launcher, the
Rusty Russelld7e28ff2007-07-19 01:49:23 -070068 * 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. */
Jes Sorensen511801d2007-10-22 11:03:31 +100070static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070071{
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
Rusty Russelldde79782007-07-26 10:41:03 -070091/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
92 * number to /dev/lguest. */
Jes Sorensen511801d2007-10-22 11:03:31 +100093static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070094{
Jes Sorensen511801d2007-10-22 11:03:31 +100095 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070096
97 if (get_user(irq, input) != 0)
98 return -EFAULT;
99 if (irq >= LGUEST_IRQS)
100 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -0700101 /* Next time the Guest runs, the core code will see if it can deliver
102 * this interrupt. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700103 set_bit(irq, lg->irqs_pending);
104 return 0;
105}
106
Rusty Russelldde79782007-07-26 10:41:03 -0700107/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
108 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700109static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
110{
111 struct lguest *lg = file->private_data;
112
Rusty Russelldde79782007-07-26 10:41:03 -0700113 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700114 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
Rusty Russelldde79782007-07-26 10:41:03 -0700121 /* If the guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700122 if (lg->dead) {
123 size_t len;
124
Rusty Russelldde79782007-07-26 10:41:03 -0700125 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700126 if (IS_ERR(lg->dead))
127 return PTR_ERR(lg->dead);
128
Rusty Russelldde79782007-07-26 10:41:03 -0700129 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700130 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
Rusty Russelldde79782007-07-26 10:41:03 -0700136 /* If we returned from read() last time because the Guest sent DMA,
137 * clear the flag. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700138 if (lg->dma_is_pending)
139 lg->dma_is_pending = 0;
140
Rusty Russelldde79782007-07-26 10:41:03 -0700141 /* Run the Guest until something interesting happens. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700142 return run_guest(lg, (unsigned long __user *)user);
143}
144
Jes Sorensen511801d2007-10-22 11:03:31 +1000145/*L:020 The initialization write supplies 5 pointer sized (32 or 64 bit)
146 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700147 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000148 * base: The start of the Guest-physical memory inside the Launcher memory.
149 *
Rusty Russelldde79782007-07-26 10:41:03 -0700150 * 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 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000165static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700166{
Rusty Russelldde79782007-07-26 10:41:03 -0700167 /* "struct lguest" contains everything we (the Host) know about a
168 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700169 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000170 int err;
Jes Sorensen511801d2007-10-22 11:03:31 +1000171 unsigned long args[5];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700172
Rusty Russell48245cc2007-10-22 11:03:27 +1000173 /* We grab the Big Lguest lock, which protects against multiple
174 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700176 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700177 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
Rusty Russell48245cc2007-10-22 11:03:27 +1000187 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
188 if (!lg) {
189 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700190 goto unlock;
191 }
Rusty Russelldde79782007-07-26 10:41:03 -0700192
193 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000194 lg->mem_base = (void __user *)(long)args[0];
195 lg->pfn_limit = args[1];
196 lg->page_offset = args[4];
Rusty Russelldde79782007-07-26 10:41:03 -0700197
198 /* We need a complete page for the Guest registers: they are accessible
199 * to the Guest and we can only grant it access to whole pages. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700200 lg->regs_page = get_zeroed_page(GFP_KERNEL);
201 if (!lg->regs_page) {
202 err = -ENOMEM;
203 goto release_guest;
204 }
Rusty Russelldde79782007-07-26 10:41:03 -0700205 /* We actually put the registers at the bottom of the page. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700206 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
207
Rusty Russelldde79782007-07-26 10:41:03 -0700208 /* Initialize the Guest's shadow page tables, using the toplevel
209 * address the Launcher gave us. This allocates memory, so can
210 * fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000211 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700212 if (err)
213 goto free_regs;
214
Rusty Russelldde79782007-07-26 10:41:03 -0700215 /* Now we initialize the Guest's registers, handing it the start
216 * address. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000217 setup_regs(lg->regs, args[3]);
Rusty Russelldde79782007-07-26 10:41:03 -0700218
219 /* There are a couple of GDT entries the Guest expects when first
220 * booting. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700221 setup_guest_gdt(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700222
223 /* The timer for lguest's clock needs initialization. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700224 init_clockdev(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700225
226 /* We keep a pointer to the Launcher task (ie. current task) for when
227 * other Guests want to wake this one (inter-Guest I/O). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228 lg->tsk = current;
Rusty Russelldde79782007-07-26 10:41:03 -0700229 /* We need to keep a pointer to the Launcher's memory map, because if
230 * the Launcher dies we need to clean it up. If we don't keep a
231 * reference, it is destroyed before close() is called. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700232 lg->mm = get_task_mm(lg->tsk);
Rusty Russelldde79782007-07-26 10:41:03 -0700233
234 /* Initialize the queue for the waker to wait on */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235 init_waitqueue_head(&lg->break_wq);
Rusty Russelldde79782007-07-26 10:41:03 -0700236
237 /* We remember which CPU's pages this Guest used last, for optimization
238 * when the same Guest runs on the same CPU twice. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239 lg->last_pages = NULL;
Rusty Russelldde79782007-07-26 10:41:03 -0700240
241 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700242 file->private_data = lg;
243
244 mutex_unlock(&lguest_lock);
245
Rusty Russelldde79782007-07-26 10:41:03 -0700246 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700247 return sizeof(args);
248
249free_regs:
250 free_page(lg->regs_page);
251release_guest:
252 memset(lg, 0, sizeof(*lg));
253unlock:
254 mutex_unlock(&lguest_lock);
255 return err;
256}
257
Rusty Russelldde79782007-07-26 10:41:03 -0700258/*L:010 The first operation the Launcher does must be a write. All writes
259 * start with a 32 bit number: for the first write this must be
260 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
261 * writes of other values to get DMA buffers and send interrupts. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000262static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700263 size_t size, loff_t *off)
264{
Rusty Russelldde79782007-07-26 10:41:03 -0700265 /* Once the guest is initialized, we hold the "struct lguest" in the
266 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700267 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000268 const unsigned long __user *input = (const unsigned long __user *)in;
269 unsigned long req;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700270
271 if (get_user(req, input) != 0)
272 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000273 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700274
Rusty Russelldde79782007-07-26 10:41:03 -0700275 /* If you haven't initialized, you must do that first. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700276 if (req != LHREQ_INITIALIZE && !lg)
277 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -0700278
279 /* Once the Guest is dead, all you can do is read() why it died. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700280 if (lg && lg->dead)
281 return -ENOENT;
282
283 /* If you're not the task which owns the Guest, you can only break */
284 if (lg && current != lg->tsk && req != LHREQ_BREAK)
285 return -EPERM;
286
287 switch (req) {
288 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000289 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700290 case LHREQ_GETDMA:
Jes Sorensen511801d2007-10-22 11:03:31 +1000291 return user_get_dma(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 case LHREQ_IRQ:
Jes Sorensen511801d2007-10-22 11:03:31 +1000293 return user_send_irq(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700294 case LHREQ_BREAK:
Jes Sorensen511801d2007-10-22 11:03:31 +1000295 return break_guest_out(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700296 default:
297 return -EINVAL;
298 }
299}
300
Rusty Russelldde79782007-07-26 10:41:03 -0700301/*L:060 The final piece of interface code is the close() routine. It reverses
302 * everything done in initialize(). This is usually called because the
303 * Launcher exited.
304 *
305 * Note that the close routine returns 0 or a negative error number: it can't
306 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
307 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700308static int close(struct inode *inode, struct file *file)
309{
310 struct lguest *lg = file->private_data;
311
Rusty Russelldde79782007-07-26 10:41:03 -0700312 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700313 if (!lg)
314 return 0;
315
Rusty Russelldde79782007-07-26 10:41:03 -0700316 /* We need the big lock, to protect from inter-guest I/O and other
317 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700318 mutex_lock(&lguest_lock);
319 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
320 hrtimer_cancel(&lg->hrt);
Rusty Russelldde79782007-07-26 10:41:03 -0700321 /* Free any DMA buffers the Guest had bound. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700322 release_all_dma(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700323 /* Free up the shadow page tables for the Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700324 free_guest_pagetable(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700325 /* Now all the memory cleanups are done, it's safe to release the
326 * Launcher's memory management structure. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700327 mmput(lg->mm);
Rusty Russelldde79782007-07-26 10:41:03 -0700328 /* If lg->dead doesn't contain an error code it will be NULL or a
329 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700330 if (!IS_ERR(lg->dead))
331 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700332 /* We can free up the register page we allocated. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700333 free_page(lg->regs_page);
Rusty Russelldde79782007-07-26 10:41:03 -0700334 /* We clear the entire structure, which also marks it as free for the
335 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700336 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700337 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700338 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700339
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700340 return 0;
341}
342
Rusty Russelldde79782007-07-26 10:41:03 -0700343/*L:000
344 * Welcome to our journey through the Launcher!
345 *
346 * The Launcher is the Host userspace program which sets up, runs and services
347 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
348 * doing things are inaccurate: the Launcher does all the device handling for
349 * the Guest. The Guest can't tell what's done by the the Launcher and what by
350 * the Host.
351 *
352 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
353 * shall see more of that later.
354 *
355 * We begin our understanding with the Host kernel interface which the Launcher
356 * uses: reading and writing a character device called /dev/lguest. All the
357 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700358static struct file_operations lguest_fops = {
359 .owner = THIS_MODULE,
360 .release = close,
361 .write = write,
362 .read = read,
363};
Rusty Russelldde79782007-07-26 10:41:03 -0700364
365/* This is a textbook example of a "misc" character device. Populate a "struct
366 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700367static struct miscdevice lguest_dev = {
368 .minor = MISC_DYNAMIC_MINOR,
369 .name = "lguest",
370 .fops = &lguest_fops,
371};
372
373int __init lguest_device_init(void)
374{
375 return misc_register(&lguest_dev);
376}
377
378void __exit lguest_device_remove(void)
379{
380 misc_deregister(&lguest_dev);
381}