diff options
Diffstat (limited to 'drivers/lguest/lguest_user.c')
-rw-r--r-- | drivers/lguest/lguest_user.c | 166 |
1 files changed, 156 insertions, 10 deletions
diff --git a/drivers/lguest/lguest_user.c b/drivers/lguest/lguest_user.c index e90d7a783daf..80d1b58c7698 100644 --- a/drivers/lguest/lguest_user.c +++ b/drivers/lguest/lguest_user.c | |||
@@ -1,36 +1,70 @@ | |||
1 | /* Userspace control of the guest, via /dev/lguest. */ | 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 memory size, pagetable, entry point and kernel address offset. | ||
4 | * A read will run the Guest until a signal is pending (-EINTR), or the Guest | ||
5 | * does a DMA out to the Launcher. Writes are also used to get a DMA buffer | ||
6 | * registered by the Guest and to send the Guest an interrupt. :*/ | ||
2 | #include <linux/uaccess.h> | 7 | #include <linux/uaccess.h> |
3 | #include <linux/miscdevice.h> | 8 | #include <linux/miscdevice.h> |
4 | #include <linux/fs.h> | 9 | #include <linux/fs.h> |
5 | #include "lg.h" | 10 | #include "lg.h" |
6 | 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. */ | ||
7 | static void setup_regs(struct lguest_regs *regs, unsigned long start) | 17 | static void setup_regs(struct lguest_regs *regs, unsigned long start) |
8 | { | 18 | { |
9 | /* 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).*/ | ||
10 | regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; | 26 | regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; |
11 | regs->cs = __KERNEL_CS|GUEST_PL; | 27 | regs->cs = __KERNEL_CS|GUEST_PL; |
12 | 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. */ | ||
13 | regs->eip = start; | 37 | regs->eip = start; |
14 | /* 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. */ | ||
15 | } | 41 | } |
16 | 42 | ||
17 | /* + 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. */ | ||
18 | static long user_get_dma(struct lguest *lg, const u32 __user *input) | 46 | static long user_get_dma(struct lguest *lg, const u32 __user *input) |
19 | { | 47 | { |
20 | unsigned long key, udma, irq; | 48 | unsigned long key, udma, irq; |
21 | 49 | ||
50 | /* Fetch the key they wrote to us. */ | ||
22 | if (get_user(key, input) != 0) | 51 | if (get_user(key, input) != 0) |
23 | return -EFAULT; | 52 | return -EFAULT; |
53 | /* Look for a free Guest DMA buffer bound to that key. */ | ||
24 | udma = get_dma_buffer(lg, key, &irq); | 54 | udma = get_dma_buffer(lg, key, &irq); |
25 | if (!udma) | 55 | if (!udma) |
26 | return -ENOENT; | 56 | return -ENOENT; |
27 | 57 | ||
28 | /* 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. */ | ||
29 | 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(). */ | ||
30 | return udma; | 64 | return udma; |
31 | } | 65 | } |
32 | 66 | ||
33 | /* 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 |
34 | * 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 |
35 | * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */ | 69 | * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */ |
36 | static int break_guest_out(struct lguest *lg, const u32 __user *input) | 70 | static int break_guest_out(struct lguest *lg, const u32 __user *input) |
@@ -54,7 +88,8 @@ static int break_guest_out(struct lguest *lg, const u32 __user *input) | |||
54 | } | 88 | } |
55 | } | 89 | } |
56 | 90 | ||
57 | /* + irq */ | 91 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
92 | * number to /dev/lguest. */ | ||
58 | static int user_send_irq(struct lguest *lg, const u32 __user *input) | 93 | static int user_send_irq(struct lguest *lg, const u32 __user *input) |
59 | { | 94 | { |
60 | u32 irq; | 95 | u32 irq; |
@@ -63,14 +98,19 @@ static int user_send_irq(struct lguest *lg, const u32 __user *input) | |||
63 | return -EFAULT; | 98 | return -EFAULT; |
64 | if (irq >= LGUEST_IRQS) | 99 | if (irq >= LGUEST_IRQS) |
65 | return -EINVAL; | 100 | return -EINVAL; |
101 | /* Next time the Guest runs, the core code will see if it can deliver | ||
102 | * this interrupt. */ | ||
66 | set_bit(irq, lg->irqs_pending); | 103 | set_bit(irq, lg->irqs_pending); |
67 | return 0; | 104 | return 0; |
68 | } | 105 | } |
69 | 106 | ||
107 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading | ||
108 | * from /dev/lguest. */ | ||
70 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | 109 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
71 | { | 110 | { |
72 | struct lguest *lg = file->private_data; | 111 | struct lguest *lg = file->private_data; |
73 | 112 | ||
113 | /* You must write LHREQ_INITIALIZE first! */ | ||
74 | if (!lg) | 114 | if (!lg) |
75 | return -EINVAL; | 115 | return -EINVAL; |
76 | 116 | ||
@@ -78,27 +118,52 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
78 | if (current != lg->tsk) | 118 | if (current != lg->tsk) |
79 | return -EPERM; | 119 | return -EPERM; |
80 | 120 | ||
121 | /* If the guest is already dead, we indicate why */ | ||
81 | if (lg->dead) { | 122 | if (lg->dead) { |
82 | size_t len; | 123 | size_t len; |
83 | 124 | ||
125 | /* lg->dead either contains an error code, or a string. */ | ||
84 | if (IS_ERR(lg->dead)) | 126 | if (IS_ERR(lg->dead)) |
85 | return PTR_ERR(lg->dead); | 127 | return PTR_ERR(lg->dead); |
86 | 128 | ||
129 | /* We can only return as much as the buffer they read with. */ | ||
87 | len = min(size, strlen(lg->dead)+1); | 130 | len = min(size, strlen(lg->dead)+1); |
88 | if (copy_to_user(user, lg->dead, len) != 0) | 131 | if (copy_to_user(user, lg->dead, len) != 0) |
89 | return -EFAULT; | 132 | return -EFAULT; |
90 | return len; | 133 | return len; |
91 | } | 134 | } |
92 | 135 | ||
136 | /* If we returned from read() last time because the Guest sent DMA, | ||
137 | * clear the flag. */ | ||
93 | if (lg->dma_is_pending) | 138 | if (lg->dma_is_pending) |
94 | lg->dma_is_pending = 0; | 139 | lg->dma_is_pending = 0; |
95 | 140 | ||
141 | /* Run the Guest until something interesting happens. */ | ||
96 | return run_guest(lg, (unsigned long __user *)user); | 142 | return run_guest(lg, (unsigned long __user *)user); |
97 | } | 143 | } |
98 | 144 | ||
99 | /* 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 | */ | ||
100 | static int initialize(struct file *file, const u32 __user *input) | 163 | static int initialize(struct file *file, const u32 __user *input) |
101 | { | 164 | { |
165 | /* "struct lguest" contains everything we (the Host) know about a | ||
166 | * Guest. */ | ||
102 | struct lguest *lg; | 167 | struct lguest *lg; |
103 | int err, i; | 168 | int err, i; |
104 | u32 args[4]; | 169 | u32 args[4]; |
@@ -106,7 +171,7 @@ static int initialize(struct file *file, const u32 __user *input) | |||
106 | /* We grab the Big Lguest lock, which protects the global array | 171 | /* We grab the Big Lguest lock, which protects the global array |
107 | * "lguests" and multiple simultaneous initializations. */ | 172 | * "lguests" and multiple simultaneous initializations. */ |
108 | mutex_lock(&lguest_lock); | 173 | mutex_lock(&lguest_lock); |
109 | 174 | /* You can't initialize twice! Close the device and start again... */ | |
110 | if (file->private_data) { | 175 | if (file->private_data) { |
111 | err = -EBUSY; | 176 | err = -EBUSY; |
112 | goto unlock; | 177 | goto unlock; |
@@ -117,37 +182,70 @@ static int initialize(struct file *file, const u32 __user *input) | |||
117 | goto unlock; | 182 | goto unlock; |
118 | } | 183 | } |
119 | 184 | ||
185 | /* Find an unused guest. */ | ||
120 | i = find_free_guest(); | 186 | i = find_free_guest(); |
121 | if (i < 0) { | 187 | if (i < 0) { |
122 | err = -ENOSPC; | 188 | err = -ENOSPC; |
123 | goto unlock; | 189 | goto unlock; |
124 | } | 190 | } |
191 | /* OK, we have an index into the "lguest" array: "lg" is a convenient | ||
192 | * pointer. */ | ||
125 | lg = &lguests[i]; | 193 | lg = &lguests[i]; |
194 | |||
195 | /* Populate the easy fields of our "struct lguest" */ | ||
126 | lg->guestid = i; | 196 | lg->guestid = i; |
127 | lg->pfn_limit = args[0]; | 197 | lg->pfn_limit = args[0]; |
128 | 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. */ | ||
129 | lg->regs_page = get_zeroed_page(GFP_KERNEL); | 202 | lg->regs_page = get_zeroed_page(GFP_KERNEL); |
130 | if (!lg->regs_page) { | 203 | if (!lg->regs_page) { |
131 | err = -ENOMEM; | 204 | err = -ENOMEM; |
132 | goto release_guest; | 205 | goto release_guest; |
133 | } | 206 | } |
207 | /* We actually put the registers at the bottom of the page. */ | ||
134 | lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs); | 208 | lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs); |
135 | 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. */ | ||
136 | err = init_guest_pagetable(lg, args[1]); | 213 | err = init_guest_pagetable(lg, args[1]); |
137 | if (err) | 214 | if (err) |
138 | goto free_regs; | 215 | goto free_regs; |
139 | 216 | ||
217 | /* Now we initialize the Guest's registers, handing it the start | ||
218 | * address. */ | ||
140 | 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. */ | ||
141 | setup_guest_gdt(lg); | 223 | setup_guest_gdt(lg); |
224 | |||
225 | /* The timer for lguest's clock needs initialization. */ | ||
142 | 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). */ | ||
143 | 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. */ | ||
144 | 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 */ | ||
145 | 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. */ | ||
146 | lg->last_pages = NULL; | 241 | lg->last_pages = NULL; |
242 | |||
243 | /* We keep our "struct lguest" in the file's private_data. */ | ||
147 | file->private_data = lg; | 244 | file->private_data = lg; |
148 | 245 | ||
149 | mutex_unlock(&lguest_lock); | 246 | mutex_unlock(&lguest_lock); |
150 | 247 | ||
248 | /* And because this is a write() call, we return the length used. */ | ||
151 | return sizeof(args); | 249 | return sizeof(args); |
152 | 250 | ||
153 | free_regs: | 251 | free_regs: |
@@ -159,9 +257,15 @@ unlock: | |||
159 | return err; | 257 | return err; |
160 | } | 258 | } |
161 | 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. */ | ||
162 | static ssize_t write(struct file *file, const char __user *input, | 264 | static ssize_t write(struct file *file, const char __user *input, |
163 | size_t size, loff_t *off) | 265 | size_t size, loff_t *off) |
164 | { | 266 | { |
267 | /* Once the guest is initialized, we hold the "struct lguest" in the | ||
268 | * file private data. */ | ||
165 | struct lguest *lg = file->private_data; | 269 | struct lguest *lg = file->private_data; |
166 | u32 req; | 270 | u32 req; |
167 | 271 | ||
@@ -169,8 +273,11 @@ static ssize_t write(struct file *file, const char __user *input, | |||
169 | return -EFAULT; | 273 | return -EFAULT; |
170 | input += sizeof(req); | 274 | input += sizeof(req); |
171 | 275 | ||
276 | /* If you haven't initialized, you must do that first. */ | ||
172 | if (req != LHREQ_INITIALIZE && !lg) | 277 | if (req != LHREQ_INITIALIZE && !lg) |
173 | return -EINVAL; | 278 | return -EINVAL; |
279 | |||
280 | /* Once the Guest is dead, all you can do is read() why it died. */ | ||
174 | if (lg && lg->dead) | 281 | if (lg && lg->dead) |
175 | return -ENOENT; | 282 | return -ENOENT; |
176 | 283 | ||
@@ -192,33 +299,72 @@ static ssize_t write(struct file *file, const char __user *input, | |||
192 | } | 299 | } |
193 | } | 300 | } |
194 | 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. :*/ | ||
195 | static int close(struct inode *inode, struct file *file) | 309 | static int close(struct inode *inode, struct file *file) |
196 | { | 310 | { |
197 | struct lguest *lg = file->private_data; | 311 | struct lguest *lg = file->private_data; |
198 | 312 | ||
313 | /* If we never successfully initialized, there's nothing to clean up */ | ||
199 | if (!lg) | 314 | if (!lg) |
200 | return 0; | 315 | return 0; |
201 | 316 | ||
317 | /* We need the big lock, to protect from inter-guest I/O and other | ||
318 | * Launchers initializing guests. */ | ||
202 | mutex_lock(&lguest_lock); | 319 | mutex_lock(&lguest_lock); |
203 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ | 320 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
204 | hrtimer_cancel(&lg->hrt); | 321 | hrtimer_cancel(&lg->hrt); |
322 | /* Free any DMA buffers the Guest had bound. */ | ||
205 | release_all_dma(lg); | 323 | release_all_dma(lg); |
324 | /* Free up the shadow page tables for the Guest. */ | ||
206 | 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. */ | ||
207 | 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(). */ | ||
208 | if (!IS_ERR(lg->dead)) | 331 | if (!IS_ERR(lg->dead)) |
209 | kfree(lg->dead); | 332 | kfree(lg->dead); |
333 | /* We can free up the register page we allocated. */ | ||
210 | 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. */ | ||
211 | memset(lg, 0, sizeof(*lg)); | 337 | memset(lg, 0, sizeof(*lg)); |
338 | /* Release lock and exit. */ | ||
212 | mutex_unlock(&lguest_lock); | 339 | mutex_unlock(&lguest_lock); |
340 | |||
213 | return 0; | 341 | return 0; |
214 | } | 342 | } |
215 | 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: */ | ||
216 | static struct file_operations lguest_fops = { | 359 | static struct file_operations lguest_fops = { |
217 | .owner = THIS_MODULE, | 360 | .owner = THIS_MODULE, |
218 | .release = close, | 361 | .release = close, |
219 | .write = write, | 362 | .write = write, |
220 | .read = read, | 363 | .read = read, |
221 | }; | 364 | }; |
365 | |||
366 | /* This is a textbook example of a "misc" character device. Populate a "struct | ||
367 | * miscdevice" and register it with misc_register(). */ | ||
222 | static struct miscdevice lguest_dev = { | 368 | static struct miscdevice lguest_dev = { |
223 | .minor = MISC_DYNAMIC_MINOR, | 369 | .minor = MISC_DYNAMIC_MINOR, |
224 | .name = "lguest", | 370 | .name = "lguest", |