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