blob: 7e92017103dc738a9320336de697154daf4858e5 [file] [log] [blame]
Rusty Russell2e04ef72009-07-30 16:03:45 -06001/*P:200
2 * This contains all the /dev/lguest code, whereby the userspace launcher
Rusty Russellf938d2c2007-07-26 10:41:02 -07003 * controls and communicates with the Guest. For example, the first write will
Rusty Russell3c6b5bf2007-10-22 11:03:26 +10004 * tell us the Guest's memory layout, pagetable, entry point and kernel address
5 * offset. A read will run the Guest until something happens, such as a signal
Rusty Russell2e04ef72009-07-30 16:03:45 -06006 * or the Guest doing a NOTIFY out to the Launcher.
7:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07008#include <linux/uaccess.h>
9#include <linux/miscdevice.h>
10#include <linux/fs.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020011#include <linux/sched.h>
Rusty Russelldf60aee2009-06-12 22:27:09 -060012#include <linux/eventfd.h>
13#include <linux/file.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070014#include "lg.h"
15
Rusty Russelldf60aee2009-06-12 22:27:09 -060016bool send_notify_to_eventfd(struct lg_cpu *cpu)
17{
18 unsigned int i;
19 struct lg_eventfd_map *map;
20
21 /* lg->eventfds is RCU-protected */
22 rcu_read_lock();
23 map = rcu_dereference(cpu->lg->eventfds);
24 for (i = 0; i < map->num; i++) {
25 if (map->map[i].addr == cpu->pending_notify) {
26 eventfd_signal(map->map[i].event, 1);
27 cpu->pending_notify = 0;
28 break;
29 }
30 }
31 rcu_read_unlock();
32 return cpu->pending_notify == 0;
33}
34
35static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
36{
37 struct lg_eventfd_map *new, *old = lg->eventfds;
38
39 if (!addr)
40 return -EINVAL;
41
Rusty Russell2e04ef72009-07-30 16:03:45 -060042 /*
43 * Replace the old array with the new one, carefully: others can
44 * be accessing it at the same time.
45 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060046 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
47 GFP_KERNEL);
48 if (!new)
49 return -ENOMEM;
50
51 /* First make identical copy. */
52 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
53 new->num = old->num;
54
55 /* Now append new entry. */
56 new->map[new->num].addr = addr;
Davide Libenzi13389012009-06-30 11:41:11 -070057 new->map[new->num].event = eventfd_ctx_fdget(fd);
Rusty Russelldf60aee2009-06-12 22:27:09 -060058 if (IS_ERR(new->map[new->num].event)) {
Dan Carpenterf2945262009-07-19 14:46:09 +030059 int err = PTR_ERR(new->map[new->num].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -060060 kfree(new);
Dan Carpenterf2945262009-07-19 14:46:09 +030061 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -060062 }
63 new->num++;
64
65 /* Now put new one in place. */
66 rcu_assign_pointer(lg->eventfds, new);
67
Rusty Russell2e04ef72009-07-30 16:03:45 -060068 /*
69 * We're not in a big hurry. Wait until noone's looking at old
70 * version, then delete it.
71 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060072 synchronize_rcu();
73 kfree(old);
74
75 return 0;
76}
77
78static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
79{
80 unsigned long addr, fd;
81 int err;
82
83 if (get_user(addr, input) != 0)
84 return -EFAULT;
85 input++;
86 if (get_user(fd, input) != 0)
87 return -EFAULT;
88
89 mutex_lock(&lguest_lock);
90 err = add_eventfd(lg, addr, fd);
91 mutex_unlock(&lguest_lock);
92
Dan Carpenterf2945262009-07-19 14:46:09 +030093 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -060094}
95
Rusty Russell2e04ef72009-07-30 16:03:45 -060096/*L:050
97 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
98 * number to /dev/lguest.
99 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200100static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700101{
Jes Sorensen511801d2007-10-22 11:03:31 +1000102 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700103
104 if (get_user(irq, input) != 0)
105 return -EFAULT;
106 if (irq >= LGUEST_IRQS)
107 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600108
109 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700110 return 0;
111}
112
Rusty Russell2e04ef72009-07-30 16:03:45 -0600113/*L:040
114 * Once our Guest is initialized, the Launcher makes it run by reading
115 * from /dev/lguest.
116 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700117static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
118{
119 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200120 struct lg_cpu *cpu;
121 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700122
Rusty Russelldde79782007-07-26 10:41:03 -0700123 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124 if (!lg)
125 return -EINVAL;
126
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200127 /* Watch out for arbitrary vcpu indexes! */
128 if (cpu_id >= lg->nr_cpus)
129 return -EINVAL;
130
131 cpu = &lg->cpus[cpu_id];
132
Rusty Russelle1e72962007-10-25 15:02:50 +1000133 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200134 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700135 return -EPERM;
136
Rusty Russella6bd8e12008-03-28 11:05:53 -0500137 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700138 if (lg->dead) {
139 size_t len;
140
Rusty Russelldde79782007-07-26 10:41:03 -0700141 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700142 if (IS_ERR(lg->dead))
143 return PTR_ERR(lg->dead);
144
Rusty Russelldde79782007-07-26 10:41:03 -0700145 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700146 len = min(size, strlen(lg->dead)+1);
147 if (copy_to_user(user, lg->dead, len) != 0)
148 return -EFAULT;
149 return len;
150 }
151
Rusty Russell2e04ef72009-07-30 16:03:45 -0600152 /*
153 * If we returned from read() last time because the Guest sent I/O,
154 * clear the flag.
155 */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200156 if (cpu->pending_notify)
157 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700158
Rusty Russelldde79782007-07-26 10:41:03 -0700159 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200160 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700161}
162
Rusty Russell2e04ef72009-07-30 16:03:45 -0600163/*L:025
164 * This actually initializes a CPU. For the moment, a Guest is only
165 * uniprocessor, so "id" is always 0.
166 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200167static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
168{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500169 /* We have a limited number the number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500170 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200171 return -EINVAL;
172
Rusty Russella6bd8e12008-03-28 11:05:53 -0500173 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200174 cpu->id = id;
175 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
176 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500177
178 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200179 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200180
Rusty Russell2e04ef72009-07-30 16:03:45 -0600181 /*
182 * We need a complete page for the Guest registers: they are accessible
183 * to the Guest and we can only grant it access to whole pages.
184 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200185 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
186 if (!cpu->regs_page)
187 return -ENOMEM;
188
189 /* We actually put the registers at the bottom of the page. */
190 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
191
Rusty Russell2e04ef72009-07-30 16:03:45 -0600192 /*
193 * Now we initialize the Guest's registers, handing it the start
194 * address.
195 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200196 lguest_arch_setup_regs(cpu, start_ip);
197
Rusty Russell2e04ef72009-07-30 16:03:45 -0600198 /*
199 * We keep a pointer to the Launcher task (ie. current task) for when
200 * other Guests want to wake this one (eg. console input).
201 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200202 cpu->tsk = current;
203
Rusty Russell2e04ef72009-07-30 16:03:45 -0600204 /*
205 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200206 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600207 * reference, it is destroyed before close() is called.
208 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200209 cpu->mm = get_task_mm(cpu->tsk);
210
Rusty Russell2e04ef72009-07-30 16:03:45 -0600211 /*
212 * We remember which CPU's pages this Guest used last, for optimization
213 * when the same Guest runs on the same CPU twice.
214 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200215 cpu->last_pages = NULL;
216
Rusty Russella6bd8e12008-03-28 11:05:53 -0500217 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200218 return 0;
219}
220
Rusty Russell2e04ef72009-07-30 16:03:45 -0600221/*L:020
222 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
223 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700224 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000225 * base: The start of the Guest-physical memory inside the Launcher memory.
226 *
Rusty Russelldde79782007-07-26 10:41:03 -0700227 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000228 * allowed to access. The Guest memory lives inside the Launcher, so it sets
229 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700230 *
Rusty Russelldde79782007-07-26 10:41:03 -0700231 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700232 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000233static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700234{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600235 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700236 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000237 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300238 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239
Rusty Russell2e04ef72009-07-30 16:03:45 -0600240 /*
241 * We grab the Big Lguest lock, which protects against multiple
242 * simultaneous initializations.
243 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700244 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700245 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700246 if (file->private_data) {
247 err = -EBUSY;
248 goto unlock;
249 }
250
251 if (copy_from_user(args, input, sizeof(args)) != 0) {
252 err = -EFAULT;
253 goto unlock;
254 }
255
Rusty Russell48245cc2007-10-22 11:03:27 +1000256 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
257 if (!lg) {
258 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700259 goto unlock;
260 }
Rusty Russelldde79782007-07-26 10:41:03 -0700261
Rusty Russelldf60aee2009-06-12 22:27:09 -0600262 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
263 if (!lg->eventfds) {
264 err = -ENOMEM;
265 goto free_lg;
266 }
267 lg->eventfds->num = 0;
268
Rusty Russelldde79782007-07-26 10:41:03 -0700269 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000270 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000271 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700272
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300273 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
274 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200275 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600276 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200277
Rusty Russell2e04ef72009-07-30 16:03:45 -0600278 /*
279 * Initialize the Guest's shadow page tables, using the toplevel
280 * address the Launcher gave us. This allocates memory, so can fail.
281 */
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 Russell2e04ef72009-07-30 16:03:45 -0600306/*L:010
307 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000308 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700309 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella6bd8e12008-03-28 11:05:53 -0500310 * writes of other values to send interrupts.
311 *
312 * Note that we overload the "offset" in the /dev/lguest file to indicate what
313 * CPU number we're dealing with. Currently this is always 0, since we only
314 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600315 * here.
316 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000317static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700318 size_t size, loff_t *off)
319{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600320 /*
321 * Once the Guest is initialized, we hold the "struct lguest" in the
322 * file private data.
323 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700324 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000325 const unsigned long __user *input = (const unsigned long __user *)in;
326 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200327 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200328 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700329
Rusty Russella6bd8e12008-03-28 11:05:53 -0500330 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700331 if (get_user(req, input) != 0)
332 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000333 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700334
Rusty Russelldde79782007-07-26 10:41:03 -0700335 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200336 if (req != LHREQ_INITIALIZE) {
337 if (!lg || (cpu_id >= lg->nr_cpus))
338 return -EINVAL;
339 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800340
341 /* Once the Guest is dead, you can only read() why it died. */
342 if (lg->dead)
343 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200344 }
Rusty Russelldde79782007-07-26 10:41:03 -0700345
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700346 switch (req) {
347 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000348 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700349 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200350 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600351 case LHREQ_EVENTFD:
352 return attach_eventfd(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700353 default:
354 return -EINVAL;
355 }
356}
357
Rusty Russell2e04ef72009-07-30 16:03:45 -0600358/*L:060
359 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700360 * everything done in initialize(). This is usually called because the
361 * Launcher exited.
362 *
363 * Note that the close routine returns 0 or a negative error number: it can't
364 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600365 * letting them do it.
366:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700367static int close(struct inode *inode, struct file *file)
368{
369 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200370 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700371
Rusty Russelldde79782007-07-26 10:41:03 -0700372 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700373 if (!lg)
374 return 0;
375
Rusty Russell2e04ef72009-07-30 16:03:45 -0600376 /*
377 * We need the big lock, to protect from inter-guest I/O and other
378 * Launchers initializing guests.
379 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700380 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200381
382 /* Free up the shadow page tables for the Guest. */
383 free_guest_pagetable(lg);
384
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200385 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200386 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
387 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200388 /* We can free up the register page we allocated. */
389 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600390 /*
391 * Now all the memory cleanups are done, it's safe to release
392 * the Launcher's memory management structure.
393 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200394 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200395 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600396
397 /* Release any eventfds they registered. */
398 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700399 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600400 kfree(lg->eventfds);
401
Rusty Russell2e04ef72009-07-30 16:03:45 -0600402 /*
403 * If lg->dead doesn't contain an error code it will be NULL or a
404 * kmalloc()ed string, either of which is ok to hand to kfree().
405 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700406 if (!IS_ERR(lg->dead))
407 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100408 /* Free the memory allocated to the lguest_struct */
409 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700410 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700411 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700412
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700413 return 0;
414}
415
Rusty Russelldde79782007-07-26 10:41:03 -0700416/*L:000
417 * Welcome to our journey through the Launcher!
418 *
419 * The Launcher is the Host userspace program which sets up, runs and services
420 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
421 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000422 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700423 *
424 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
425 * shall see more of that later.
426 *
427 * We begin our understanding with the Host kernel interface which the Launcher
428 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600429 * work happens in the read(), write() and close() routines:
430 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700431static struct file_operations lguest_fops = {
432 .owner = THIS_MODULE,
433 .release = close,
434 .write = write,
435 .read = read,
436};
Rusty Russelldde79782007-07-26 10:41:03 -0700437
Rusty Russell2e04ef72009-07-30 16:03:45 -0600438/*
439 * This is a textbook example of a "misc" character device. Populate a "struct
440 * miscdevice" and register it with misc_register().
441 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700442static struct miscdevice lguest_dev = {
443 .minor = MISC_DYNAMIC_MINOR,
444 .name = "lguest",
445 .fops = &lguest_fops,
446};
447
448int __init lguest_device_init(void)
449{
450 return misc_register(&lguest_dev);
451}
452
453void __exit lguest_device_remove(void)
454{
455 misc_deregister(&lguest_dev);
456}