blob: f6bf255f183730eccf4f00065e3673d8db6119ac [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
Rusty Russell15045272007-10-22 11:24:10 +10005 * or the Guest doing a NOTIFY out to the Launcher. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07006#include <linux/uaccess.h>
7#include <linux/miscdevice.h>
8#include <linux/fs.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -02009#include <linux/sched.h>
Rusty Russelldf60aee2009-06-12 22:27:09 -060010#include <linux/eventfd.h>
11#include <linux/file.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070012#include "lg.h"
13
Rusty Russelle1e72962007-10-25 15:02:50 +100014/*L:055 When something happens, the Waker process needs a way to stop the
15 * kernel running the Guest and return to the Launcher. So the Waker writes
16 * LHREQ_BREAK and the value "1" to /dev/lguest to do this. Once the Launcher
17 * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
18 * the Waker. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020019static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070020{
21 unsigned long on;
22
Rusty Russelle1e72962007-10-25 15:02:50 +100023 /* Fetch whether they're turning break on or off. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070024 if (get_user(on, input) != 0)
25 return -EFAULT;
26
27 if (on) {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020028 cpu->break_out = 1;
Rusty Russella6c372d2009-06-12 22:27:01 -060029 if (!wake_up_process(cpu->tsk))
30 kick_process(cpu->tsk);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070031 /* Wait for them to reset it */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020032 return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070033 } else {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020034 cpu->break_out = 0;
35 wake_up(&cpu->break_wq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070036 return 0;
37 }
38}
39
Rusty Russelldf60aee2009-06-12 22:27:09 -060040bool send_notify_to_eventfd(struct lg_cpu *cpu)
41{
42 unsigned int i;
43 struct lg_eventfd_map *map;
44
45 /* lg->eventfds is RCU-protected */
46 rcu_read_lock();
47 map = rcu_dereference(cpu->lg->eventfds);
48 for (i = 0; i < map->num; i++) {
49 if (map->map[i].addr == cpu->pending_notify) {
50 eventfd_signal(map->map[i].event, 1);
51 cpu->pending_notify = 0;
52 break;
53 }
54 }
55 rcu_read_unlock();
56 return cpu->pending_notify == 0;
57}
58
59static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
60{
61 struct lg_eventfd_map *new, *old = lg->eventfds;
62
63 if (!addr)
64 return -EINVAL;
65
66 /* Replace the old array with the new one, carefully: others can
67 * be accessing it at the same time */
68 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
69 GFP_KERNEL);
70 if (!new)
71 return -ENOMEM;
72
73 /* First make identical copy. */
74 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
75 new->num = old->num;
76
77 /* Now append new entry. */
78 new->map[new->num].addr = addr;
79 new->map[new->num].event = eventfd_fget(fd);
80 if (IS_ERR(new->map[new->num].event)) {
81 kfree(new);
82 return PTR_ERR(new->map[new->num].event);
83 }
84 new->num++;
85
86 /* Now put new one in place. */
87 rcu_assign_pointer(lg->eventfds, new);
88
89 /* We're not in a big hurry. Wait until noone's looking at old
90 * version, then delete it. */
91 synchronize_rcu();
92 kfree(old);
93
94 return 0;
95}
96
97static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
98{
99 unsigned long addr, fd;
100 int err;
101
102 if (get_user(addr, input) != 0)
103 return -EFAULT;
104 input++;
105 if (get_user(fd, input) != 0)
106 return -EFAULT;
107
108 mutex_lock(&lguest_lock);
109 err = add_eventfd(lg, addr, fd);
110 mutex_unlock(&lguest_lock);
111
112 return 0;
113}
114
Rusty Russelldde79782007-07-26 10:41:03 -0700115/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
116 * number to /dev/lguest. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200117static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700118{
Jes Sorensen511801d2007-10-22 11:03:31 +1000119 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700120
121 if (get_user(irq, input) != 0)
122 return -EFAULT;
123 if (irq >= LGUEST_IRQS)
124 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600125
126 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700127 return 0;
128}
129
Rusty Russelldde79782007-07-26 10:41:03 -0700130/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
131 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700132static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
133{
134 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200135 struct lg_cpu *cpu;
136 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700137
Rusty Russelldde79782007-07-26 10:41:03 -0700138 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700139 if (!lg)
140 return -EINVAL;
141
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200142 /* Watch out for arbitrary vcpu indexes! */
143 if (cpu_id >= lg->nr_cpus)
144 return -EINVAL;
145
146 cpu = &lg->cpus[cpu_id];
147
Rusty Russelle1e72962007-10-25 15:02:50 +1000148 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200149 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700150 return -EPERM;
151
Rusty Russella6bd8e12008-03-28 11:05:53 -0500152 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700153 if (lg->dead) {
154 size_t len;
155
Rusty Russelldde79782007-07-26 10:41:03 -0700156 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700157 if (IS_ERR(lg->dead))
158 return PTR_ERR(lg->dead);
159
Rusty Russelldde79782007-07-26 10:41:03 -0700160 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700161 len = min(size, strlen(lg->dead)+1);
162 if (copy_to_user(user, lg->dead, len) != 0)
163 return -EFAULT;
164 return len;
165 }
166
Rusty Russella6bd8e12008-03-28 11:05:53 -0500167 /* If we returned from read() last time because the Guest sent I/O,
Rusty Russelldde79782007-07-26 10:41:03 -0700168 * clear the flag. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200169 if (cpu->pending_notify)
170 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700171
Rusty Russelldde79782007-07-26 10:41:03 -0700172 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200173 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700174}
175
Rusty Russella6bd8e12008-03-28 11:05:53 -0500176/*L:025 This actually initializes a CPU. For the moment, a Guest is only
177 * uniprocessor, so "id" is always 0. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200178static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
179{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500180 /* We have a limited number the number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500181 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200182 return -EINVAL;
183
Rusty Russella6bd8e12008-03-28 11:05:53 -0500184 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200185 cpu->id = id;
186 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
187 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500188
189 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200190 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200191
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200192 /* We need a complete page for the Guest registers: they are accessible
193 * to the Guest and we can only grant it access to whole pages. */
194 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
195 if (!cpu->regs_page)
196 return -ENOMEM;
197
198 /* We actually put the registers at the bottom of the page. */
199 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
200
201 /* Now we initialize the Guest's registers, handing it the start
202 * address. */
203 lguest_arch_setup_regs(cpu, start_ip);
204
Rusty Russella6bd8e12008-03-28 11:05:53 -0500205 /* Initialize the queue for the Waker to wait on */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200206 init_waitqueue_head(&cpu->break_wq);
207
208 /* We keep a pointer to the Launcher task (ie. current task) for when
Rusty Russella6bd8e12008-03-28 11:05:53 -0500209 * other Guests want to wake this one (eg. console input). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200210 cpu->tsk = current;
211
212 /* We need to keep a pointer to the Launcher's memory map, because if
213 * the Launcher dies we need to clean it up. If we don't keep a
214 * reference, it is destroyed before close() is called. */
215 cpu->mm = get_task_mm(cpu->tsk);
216
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200217 /* We remember which CPU's pages this Guest used last, for optimization
218 * when the same Guest runs on the same CPU twice. */
219 cpu->last_pages = NULL;
220
Rusty Russella6bd8e12008-03-28 11:05:53 -0500221 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200222 return 0;
223}
224
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300225/*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000226 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700227 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000228 * base: The start of the Guest-physical memory inside the Launcher memory.
229 *
Rusty Russelldde79782007-07-26 10:41:03 -0700230 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000231 * allowed to access. The Guest memory lives inside the Launcher, so it sets
232 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700233 *
Rusty Russelldde79782007-07-26 10:41:03 -0700234 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700235 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000236static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700237{
Rusty Russelldde79782007-07-26 10:41:03 -0700238 /* "struct lguest" contains everything we (the Host) know about a
239 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700240 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000241 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300242 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700243
Rusty Russell48245cc2007-10-22 11:03:27 +1000244 /* We grab the Big Lguest lock, which protects against multiple
245 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700246 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700247 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700248 if (file->private_data) {
249 err = -EBUSY;
250 goto unlock;
251 }
252
253 if (copy_from_user(args, input, sizeof(args)) != 0) {
254 err = -EFAULT;
255 goto unlock;
256 }
257
Rusty Russell48245cc2007-10-22 11:03:27 +1000258 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
259 if (!lg) {
260 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700261 goto unlock;
262 }
Rusty Russelldde79782007-07-26 10:41:03 -0700263
Rusty Russelldf60aee2009-06-12 22:27:09 -0600264 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
265 if (!lg->eventfds) {
266 err = -ENOMEM;
267 goto free_lg;
268 }
269 lg->eventfds->num = 0;
270
Rusty Russelldde79782007-07-26 10:41:03 -0700271 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000272 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000273 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700274
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300275 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
276 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200277 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600278 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200279
Rusty Russelldde79782007-07-26 10:41:03 -0700280 /* Initialize the Guest's shadow page tables, using the toplevel
Rusty Russella6bd8e12008-03-28 11:05:53 -0500281 * address the Launcher gave us. This allocates memory, so can fail. */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300282 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700283 if (err)
284 goto free_regs;
285
Rusty Russelldde79782007-07-26 10:41:03 -0700286 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700287 file->private_data = lg;
288
289 mutex_unlock(&lguest_lock);
290
Rusty Russelldde79782007-07-26 10:41:03 -0700291 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 return sizeof(args);
293
294free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200295 /* FIXME: This should be in free_vcpu */
296 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600297free_eventfds:
298 kfree(lg->eventfds);
299free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800300 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700301unlock:
302 mutex_unlock(&lguest_lock);
303 return err;
304}
305
Rusty Russelldde79782007-07-26 10:41:03 -0700306/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000307 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700308 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella6bd8e12008-03-28 11:05:53 -0500309 * writes of other values to send interrupts.
310 *
311 * Note that we overload the "offset" in the /dev/lguest file to indicate what
312 * CPU number we're dealing with. Currently this is always 0, since we only
313 * support uniprocessor Guests, but you can see the beginnings of SMP support
314 * here. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000315static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700316 size_t size, loff_t *off)
317{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500318 /* Once the Guest is initialized, we hold the "struct lguest" in the
Rusty Russelldde79782007-07-26 10:41:03 -0700319 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700320 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000321 const unsigned long __user *input = (const unsigned long __user *)in;
322 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200323 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200324 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700325
Rusty Russella6bd8e12008-03-28 11:05:53 -0500326 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700327 if (get_user(req, input) != 0)
328 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000329 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700330
Rusty Russelldde79782007-07-26 10:41:03 -0700331 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200332 if (req != LHREQ_INITIALIZE) {
333 if (!lg || (cpu_id >= lg->nr_cpus))
334 return -EINVAL;
335 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800336
337 /* Once the Guest is dead, you can only read() why it died. */
338 if (lg->dead)
339 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200340 }
Rusty Russelldde79782007-07-26 10:41:03 -0700341
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700342 switch (req) {
343 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000344 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700345 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200346 return user_send_irq(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700347 case LHREQ_BREAK:
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200348 return break_guest_out(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600349 case LHREQ_EVENTFD:
350 return attach_eventfd(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700351 default:
352 return -EINVAL;
353 }
354}
355
Rusty Russelldde79782007-07-26 10:41:03 -0700356/*L:060 The final piece of interface code is the close() routine. It reverses
357 * everything done in initialize(). This is usually called because the
358 * Launcher exited.
359 *
360 * Note that the close routine returns 0 or a negative error number: it can't
361 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
362 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700363static int close(struct inode *inode, struct file *file)
364{
365 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200366 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700367
Rusty Russelldde79782007-07-26 10:41:03 -0700368 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700369 if (!lg)
370 return 0;
371
Rusty Russelldde79782007-07-26 10:41:03 -0700372 /* We need the big lock, to protect from inter-guest I/O and other
373 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700374 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200375
376 /* Free up the shadow page tables for the Guest. */
377 free_guest_pagetable(lg);
378
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200379 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200380 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
381 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200382 /* We can free up the register page we allocated. */
383 free_page(lg->cpus[i].regs_page);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200384 /* Now all the memory cleanups are done, it's safe to release
385 * the Launcher's memory management structure. */
386 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200387 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600388
389 /* Release any eventfds they registered. */
390 for (i = 0; i < lg->eventfds->num; i++)
391 fput(lg->eventfds->map[i].event);
392 kfree(lg->eventfds);
393
Rusty Russelldde79782007-07-26 10:41:03 -0700394 /* If lg->dead doesn't contain an error code it will be NULL or a
395 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700396 if (!IS_ERR(lg->dead))
397 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100398 /* Free the memory allocated to the lguest_struct */
399 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700400 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700401 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700402
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700403 return 0;
404}
405
Rusty Russelldde79782007-07-26 10:41:03 -0700406/*L:000
407 * Welcome to our journey through the Launcher!
408 *
409 * The Launcher is the Host userspace program which sets up, runs and services
410 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
411 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000412 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700413 *
414 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
415 * shall see more of that later.
416 *
417 * We begin our understanding with the Host kernel interface which the Launcher
418 * uses: reading and writing a character device called /dev/lguest. All the
419 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700420static struct file_operations lguest_fops = {
421 .owner = THIS_MODULE,
422 .release = close,
423 .write = write,
424 .read = read,
425};
Rusty Russelldde79782007-07-26 10:41:03 -0700426
427/* This is a textbook example of a "misc" character device. Populate a "struct
428 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700429static struct miscdevice lguest_dev = {
430 .minor = MISC_DYNAMIC_MINOR,
431 .name = "lguest",
432 .fops = &lguest_fops,
433};
434
435int __init lguest_device_init(void)
436{
437 return misc_register(&lguest_dev);
438}
439
440void __exit lguest_device_remove(void)
441{
442 misc_deregister(&lguest_dev);
443}