Commit | Line | Data |
---|---|---|
9f54288d RR |
1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace |
2 | * launcher controls and communicates with the Guest. For example, | |
3 | * the first write will tell us the Guest's memory layout and entry | |
4 | * point. A read will run the Guest until something happens, such as | |
5 | * a signal or the Guest doing a NOTIFY out to the Launcher. There is | |
6 | * also a way for the Launcher to attach eventfds to particular NOTIFY | |
7 | * values instead of returning from the read() call. | |
2e04ef76 | 8 | :*/ |
d7e28ffe RR |
9 | #include <linux/uaccess.h> |
10 | #include <linux/miscdevice.h> | |
11 | #include <linux/fs.h> | |
ca94f2bd | 12 | #include <linux/sched.h> |
df60aeef RR |
13 | #include <linux/eventfd.h> |
14 | #include <linux/file.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
39a0e33d | 16 | #include <linux/export.h> |
d7e28ffe RR |
17 | #include "lg.h" |
18 | ||
a91d74a3 RR |
19 | /*L:056 |
20 | * Before we move on, let's jump ahead and look at what the kernel does when | |
21 | * it needs to look up the eventfds. That will complete our picture of how we | |
22 | * use RCU. | |
23 | * | |
24 | * The notification value is in cpu->pending_notify: we return true if it went | |
25 | * to an eventfd. | |
26 | */ | |
df60aeef RR |
27 | bool send_notify_to_eventfd(struct lg_cpu *cpu) |
28 | { | |
29 | unsigned int i; | |
30 | struct lg_eventfd_map *map; | |
31 | ||
a91d74a3 RR |
32 | /* |
33 | * This "rcu_read_lock()" helps track when someone is still looking at | |
34 | * the (RCU-using) eventfds array. It's not actually a lock at all; | |
35 | * indeed it's a noop in many configurations. (You didn't expect me to | |
36 | * explain all the RCU secrets here, did you?) | |
37 | */ | |
df60aeef | 38 | rcu_read_lock(); |
a91d74a3 RR |
39 | /* |
40 | * rcu_dereference is the counter-side of rcu_assign_pointer(); it | |
41 | * makes sure we don't access the memory pointed to by | |
42 | * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy, | |
43 | * but Alpha allows this! Paul McKenney points out that a really | |
44 | * aggressive compiler could have the same effect: | |
45 | * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html | |
46 | * | |
47 | * So play safe, use rcu_dereference to get the rcu-protected pointer: | |
48 | */ | |
df60aeef | 49 | map = rcu_dereference(cpu->lg->eventfds); |
a91d74a3 RR |
50 | /* |
51 | * Simple array search: even if they add an eventfd while we do this, | |
52 | * we'll continue to use the old array and just won't see the new one. | |
53 | */ | |
df60aeef RR |
54 | for (i = 0; i < map->num; i++) { |
55 | if (map->map[i].addr == cpu->pending_notify) { | |
56 | eventfd_signal(map->map[i].event, 1); | |
57 | cpu->pending_notify = 0; | |
58 | break; | |
59 | } | |
60 | } | |
a91d74a3 | 61 | /* We're done with the rcu-protected variable cpu->lg->eventfds. */ |
df60aeef | 62 | rcu_read_unlock(); |
a91d74a3 RR |
63 | |
64 | /* If we cleared the notification, it's because we found a match. */ | |
df60aeef RR |
65 | return cpu->pending_notify == 0; |
66 | } | |
67 | ||
a91d74a3 RR |
68 | /*L:055 |
69 | * One of the more tricksy tricks in the Linux Kernel is a technique called | |
70 | * Read Copy Update. Since one point of lguest is to teach lguest journeyers | |
71 | * about kernel coding, I use it here. (In case you're curious, other purposes | |
72 | * include learning about virtualization and instilling a deep appreciation for | |
73 | * simplicity and puppies). | |
74 | * | |
75 | * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we | |
76 | * add new eventfds without ever blocking readers from accessing the array. | |
77 | * The current Launcher only does this during boot, so that never happens. But | |
78 | * Read Copy Update is cool, and adding a lock risks damaging even more puppies | |
79 | * than this code does. | |
80 | * | |
81 | * We allocate a brand new one-larger array, copy the old one and add our new | |
82 | * element. Then we make the lg eventfd pointer point to the new array. | |
83 | * That's the easy part: now we need to free the old one, but we need to make | |
84 | * sure no slow CPU somewhere is still looking at it. That's what | |
85 | * synchronize_rcu does for us: waits until every CPU has indicated that it has | |
86 | * moved on to know it's no longer using the old one. | |
87 | * | |
88 | * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update. | |
89 | */ | |
df60aeef RR |
90 | static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) |
91 | { | |
92 | struct lg_eventfd_map *new, *old = lg->eventfds; | |
93 | ||
a91d74a3 RR |
94 | /* |
95 | * We don't allow notifications on value 0 anyway (pending_notify of | |
96 | * 0 means "nothing pending"). | |
97 | */ | |
df60aeef RR |
98 | if (!addr) |
99 | return -EINVAL; | |
100 | ||
2e04ef76 RR |
101 | /* |
102 | * Replace the old array with the new one, carefully: others can | |
103 | * be accessing it at the same time. | |
104 | */ | |
df60aeef RR |
105 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
106 | GFP_KERNEL); | |
107 | if (!new) | |
108 | return -ENOMEM; | |
109 | ||
110 | /* First make identical copy. */ | |
111 | memcpy(new->map, old->map, sizeof(old->map[0]) * old->num); | |
112 | new->num = old->num; | |
113 | ||
114 | /* Now append new entry. */ | |
115 | new->map[new->num].addr = addr; | |
13389010 | 116 | new->map[new->num].event = eventfd_ctx_fdget(fd); |
df60aeef | 117 | if (IS_ERR(new->map[new->num].event)) { |
f2945262 | 118 | int err = PTR_ERR(new->map[new->num].event); |
df60aeef | 119 | kfree(new); |
f2945262 | 120 | return err; |
df60aeef RR |
121 | } |
122 | new->num++; | |
123 | ||
a91d74a3 RR |
124 | /* |
125 | * Now put new one in place: rcu_assign_pointer() is a fancy way of | |
126 | * doing "lg->eventfds = new", but it uses memory barriers to make | |
127 | * absolutely sure that the contents of "new" written above is nailed | |
128 | * down before we actually do the assignment. | |
129 | * | |
130 | * We have to think about these kinds of things when we're operating on | |
131 | * live data without locks. | |
132 | */ | |
df60aeef RR |
133 | rcu_assign_pointer(lg->eventfds, new); |
134 | ||
2e04ef76 | 135 | /* |
25985edc | 136 | * We're not in a big hurry. Wait until no one's looking at old |
a91d74a3 | 137 | * version, then free it. |
2e04ef76 | 138 | */ |
df60aeef RR |
139 | synchronize_rcu(); |
140 | kfree(old); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
a91d74a3 RR |
145 | /*L:052 |
146 | * Receiving notifications from the Guest is usually done by attaching a | |
147 | * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will | |
148 | * become readable when the Guest does an LHCALL_NOTIFY with that value. | |
149 | * | |
150 | * This is really convenient for processing each virtqueue in a separate | |
151 | * thread. | |
152 | */ | |
df60aeef RR |
153 | static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) |
154 | { | |
155 | unsigned long addr, fd; | |
156 | int err; | |
157 | ||
158 | if (get_user(addr, input) != 0) | |
159 | return -EFAULT; | |
160 | input++; | |
161 | if (get_user(fd, input) != 0) | |
162 | return -EFAULT; | |
163 | ||
a91d74a3 RR |
164 | /* |
165 | * Just make sure two callers don't add eventfds at once. We really | |
166 | * only need to lock against callers adding to the same Guest, so using | |
167 | * the Big Lguest Lock is overkill. But this is setup, not a fast path. | |
168 | */ | |
df60aeef RR |
169 | mutex_lock(&lguest_lock); |
170 | err = add_eventfd(lg, addr, fd); | |
171 | mutex_unlock(&lguest_lock); | |
172 | ||
f2945262 | 173 | return err; |
df60aeef RR |
174 | } |
175 | ||
2e04ef76 RR |
176 | /*L:050 |
177 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt | |
178 | * number to /dev/lguest. | |
179 | */ | |
177e449d | 180 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
d7e28ffe | 181 | { |
511801dc | 182 | unsigned long irq; |
d7e28ffe RR |
183 | |
184 | if (get_user(irq, input) != 0) | |
185 | return -EFAULT; | |
186 | if (irq >= LGUEST_IRQS) | |
187 | return -EINVAL; | |
9f155a9b | 188 | |
a91d74a3 RR |
189 | /* |
190 | * Next time the Guest runs, the core code will see if it can deliver | |
191 | * this interrupt. | |
192 | */ | |
9f155a9b | 193 | set_interrupt(cpu, irq); |
d7e28ffe RR |
194 | return 0; |
195 | } | |
196 | ||
2e04ef76 RR |
197 | /*L:040 |
198 | * Once our Guest is initialized, the Launcher makes it run by reading | |
199 | * from /dev/lguest. | |
200 | */ | |
d7e28ffe RR |
201 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
202 | { | |
203 | struct lguest *lg = file->private_data; | |
d0953d42 GOC |
204 | struct lg_cpu *cpu; |
205 | unsigned int cpu_id = *o; | |
d7e28ffe | 206 | |
dde79789 | 207 | /* You must write LHREQ_INITIALIZE first! */ |
d7e28ffe RR |
208 | if (!lg) |
209 | return -EINVAL; | |
210 | ||
d0953d42 GOC |
211 | /* Watch out for arbitrary vcpu indexes! */ |
212 | if (cpu_id >= lg->nr_cpus) | |
213 | return -EINVAL; | |
214 | ||
215 | cpu = &lg->cpus[cpu_id]; | |
216 | ||
e1e72965 | 217 | /* If you're not the task which owns the Guest, go away. */ |
66686c2a | 218 | if (current != cpu->tsk) |
d7e28ffe RR |
219 | return -EPERM; |
220 | ||
a6bd8e13 | 221 | /* If the Guest is already dead, we indicate why */ |
d7e28ffe RR |
222 | if (lg->dead) { |
223 | size_t len; | |
224 | ||
dde79789 | 225 | /* lg->dead either contains an error code, or a string. */ |
d7e28ffe RR |
226 | if (IS_ERR(lg->dead)) |
227 | return PTR_ERR(lg->dead); | |
228 | ||
dde79789 | 229 | /* We can only return as much as the buffer they read with. */ |
d7e28ffe RR |
230 | len = min(size, strlen(lg->dead)+1); |
231 | if (copy_to_user(user, lg->dead, len) != 0) | |
232 | return -EFAULT; | |
233 | return len; | |
234 | } | |
235 | ||
2e04ef76 RR |
236 | /* |
237 | * If we returned from read() last time because the Guest sent I/O, | |
238 | * clear the flag. | |
239 | */ | |
5e232f4f GOC |
240 | if (cpu->pending_notify) |
241 | cpu->pending_notify = 0; | |
d7e28ffe | 242 | |
dde79789 | 243 | /* Run the Guest until something interesting happens. */ |
d0953d42 | 244 | return run_guest(cpu, (unsigned long __user *)user); |
d7e28ffe RR |
245 | } |
246 | ||
2e04ef76 RR |
247 | /*L:025 |
248 | * This actually initializes a CPU. For the moment, a Guest is only | |
249 | * uniprocessor, so "id" is always 0. | |
250 | */ | |
4dcc53da GOC |
251 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
252 | { | |
c2ecd515 | 253 | /* We have a limited number of CPUs in the lguest struct. */ |
24adf127 | 254 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
4dcc53da GOC |
255 | return -EINVAL; |
256 | ||
a6bd8e13 | 257 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
4dcc53da | 258 | cpu->id = id; |
c2ecd515 | 259 | cpu->lg = container_of(cpu, struct lguest, cpus[id]); |
4dcc53da | 260 | cpu->lg->nr_cpus++; |
a6bd8e13 RR |
261 | |
262 | /* Each CPU has a timer it can set. */ | |
ad8d8f3b | 263 | init_clockdev(cpu); |
4dcc53da | 264 | |
2e04ef76 RR |
265 | /* |
266 | * We need a complete page for the Guest registers: they are accessible | |
267 | * to the Guest and we can only grant it access to whole pages. | |
268 | */ | |
a53a35a8 GOC |
269 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
270 | if (!cpu->regs_page) | |
271 | return -ENOMEM; | |
272 | ||
c2ecd515 | 273 | /* We actually put the registers at the end of the page. */ |
a53a35a8 GOC |
274 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
275 | ||
2e04ef76 RR |
276 | /* |
277 | * Now we initialize the Guest's registers, handing it the start | |
278 | * address. | |
279 | */ | |
a53a35a8 GOC |
280 | lguest_arch_setup_regs(cpu, start_ip); |
281 | ||
2e04ef76 RR |
282 | /* |
283 | * We keep a pointer to the Launcher task (ie. current task) for when | |
284 | * other Guests want to wake this one (eg. console input). | |
285 | */ | |
66686c2a GOC |
286 | cpu->tsk = current; |
287 | ||
2e04ef76 RR |
288 | /* |
289 | * We need to keep a pointer to the Launcher's memory map, because if | |
66686c2a | 290 | * the Launcher dies we need to clean it up. If we don't keep a |
2e04ef76 RR |
291 | * reference, it is destroyed before close() is called. |
292 | */ | |
66686c2a GOC |
293 | cpu->mm = get_task_mm(cpu->tsk); |
294 | ||
2e04ef76 RR |
295 | /* |
296 | * We remember which CPU's pages this Guest used last, for optimization | |
297 | * when the same Guest runs on the same CPU twice. | |
298 | */ | |
f34f8c5f GOC |
299 | cpu->last_pages = NULL; |
300 | ||
a6bd8e13 | 301 | /* No error == success. */ |
4dcc53da GOC |
302 | return 0; |
303 | } | |
304 | ||
2e04ef76 RR |
305 | /*L:020 |
306 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in | |
307 | * addition to the LHREQ_INITIALIZE value). These are: | |
dde79789 | 308 | * |
3c6b5bfa RR |
309 | * base: The start of the Guest-physical memory inside the Launcher memory. |
310 | * | |
dde79789 | 311 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
e1e72965 RR |
312 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
313 | * this to ensure the Guest can only reach its own memory. | |
dde79789 | 314 | * |
dde79789 | 315 | * start: The first instruction to execute ("eip" in x86-speak). |
dde79789 | 316 | */ |
511801dc | 317 | static int initialize(struct file *file, const unsigned long __user *input) |
d7e28ffe | 318 | { |
2e04ef76 | 319 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
d7e28ffe | 320 | struct lguest *lg; |
48245cc0 | 321 | int err; |
58a24566 | 322 | unsigned long args[3]; |
d7e28ffe | 323 | |
2e04ef76 RR |
324 | /* |
325 | * We grab the Big Lguest lock, which protects against multiple | |
326 | * simultaneous initializations. | |
327 | */ | |
d7e28ffe | 328 | mutex_lock(&lguest_lock); |
dde79789 | 329 | /* You can't initialize twice! Close the device and start again... */ |
d7e28ffe RR |
330 | if (file->private_data) { |
331 | err = -EBUSY; | |
332 | goto unlock; | |
333 | } | |
334 | ||
335 | if (copy_from_user(args, input, sizeof(args)) != 0) { | |
336 | err = -EFAULT; | |
337 | goto unlock; | |
338 | } | |
339 | ||
48245cc0 RR |
340 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
341 | if (!lg) { | |
342 | err = -ENOMEM; | |
d7e28ffe RR |
343 | goto unlock; |
344 | } | |
dde79789 | 345 | |
df60aeef RR |
346 | lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL); |
347 | if (!lg->eventfds) { | |
348 | err = -ENOMEM; | |
349 | goto free_lg; | |
350 | } | |
351 | lg->eventfds->num = 0; | |
352 | ||
dde79789 | 353 | /* Populate the easy fields of our "struct lguest" */ |
74dbf719 | 354 | lg->mem_base = (void __user *)args[0]; |
3c6b5bfa | 355 | lg->pfn_limit = args[1]; |
dde79789 | 356 | |
58a24566 MZ |
357 | /* This is the first cpu (cpu 0) and it will start booting at args[2] */ |
358 | err = lg_cpu_start(&lg->cpus[0], 0, args[2]); | |
4dcc53da | 359 | if (err) |
df60aeef | 360 | goto free_eventfds; |
4dcc53da | 361 | |
2e04ef76 | 362 | /* |
9f54288d RR |
363 | * Initialize the Guest's shadow page tables. This allocates |
364 | * memory, so can fail. | |
2e04ef76 | 365 | */ |
58a24566 | 366 | err = init_guest_pagetable(lg); |
d7e28ffe RR |
367 | if (err) |
368 | goto free_regs; | |
369 | ||
dde79789 | 370 | /* We keep our "struct lguest" in the file's private_data. */ |
d7e28ffe RR |
371 | file->private_data = lg; |
372 | ||
373 | mutex_unlock(&lguest_lock); | |
374 | ||
dde79789 | 375 | /* And because this is a write() call, we return the length used. */ |
d7e28ffe RR |
376 | return sizeof(args); |
377 | ||
378 | free_regs: | |
a53a35a8 GOC |
379 | /* FIXME: This should be in free_vcpu */ |
380 | free_page(lg->cpus[0].regs_page); | |
df60aeef RR |
381 | free_eventfds: |
382 | kfree(lg->eventfds); | |
383 | free_lg: | |
43054412 | 384 | kfree(lg); |
d7e28ffe RR |
385 | unlock: |
386 | mutex_unlock(&lguest_lock); | |
387 | return err; | |
388 | } | |
389 | ||
2e04ef76 RR |
390 | /*L:010 |
391 | * The first operation the Launcher does must be a write. All writes | |
e1e72965 | 392 | * start with an unsigned long number: for the first write this must be |
dde79789 | 393 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
a91d74a3 | 394 | * writes of other values to send interrupts or set up receipt of notifications. |
a6bd8e13 RR |
395 | * |
396 | * Note that we overload the "offset" in the /dev/lguest file to indicate what | |
a91d74a3 | 397 | * CPU number we're dealing with. Currently this is always 0 since we only |
a6bd8e13 | 398 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
2e04ef76 RR |
399 | * here. |
400 | */ | |
511801dc | 401 | static ssize_t write(struct file *file, const char __user *in, |
d7e28ffe RR |
402 | size_t size, loff_t *off) |
403 | { | |
2e04ef76 RR |
404 | /* |
405 | * Once the Guest is initialized, we hold the "struct lguest" in the | |
406 | * file private data. | |
407 | */ | |
d7e28ffe | 408 | struct lguest *lg = file->private_data; |
511801dc JS |
409 | const unsigned long __user *input = (const unsigned long __user *)in; |
410 | unsigned long req; | |
177e449d | 411 | struct lg_cpu *uninitialized_var(cpu); |
7ea07a15 | 412 | unsigned int cpu_id = *off; |
d7e28ffe | 413 | |
a6bd8e13 | 414 | /* The first value tells us what this request is. */ |
d7e28ffe RR |
415 | if (get_user(req, input) != 0) |
416 | return -EFAULT; | |
511801dc | 417 | input++; |
d7e28ffe | 418 | |
dde79789 | 419 | /* If you haven't initialized, you must do that first. */ |
7ea07a15 GOC |
420 | if (req != LHREQ_INITIALIZE) { |
421 | if (!lg || (cpu_id >= lg->nr_cpus)) | |
422 | return -EINVAL; | |
423 | cpu = &lg->cpus[cpu_id]; | |
dde79789 | 424 | |
f73d1e6c ET |
425 | /* Once the Guest is dead, you can only read() why it died. */ |
426 | if (lg->dead) | |
427 | return -ENOENT; | |
f73d1e6c | 428 | } |
d7e28ffe RR |
429 | |
430 | switch (req) { | |
431 | case LHREQ_INITIALIZE: | |
511801dc | 432 | return initialize(file, input); |
d7e28ffe | 433 | case LHREQ_IRQ: |
177e449d | 434 | return user_send_irq(cpu, input); |
df60aeef RR |
435 | case LHREQ_EVENTFD: |
436 | return attach_eventfd(lg, input); | |
d7e28ffe RR |
437 | default: |
438 | return -EINVAL; | |
439 | } | |
440 | } | |
441 | ||
2e04ef76 RR |
442 | /*L:060 |
443 | * The final piece of interface code is the close() routine. It reverses | |
dde79789 RR |
444 | * everything done in initialize(). This is usually called because the |
445 | * Launcher exited. | |
446 | * | |
447 | * Note that the close routine returns 0 or a negative error number: it can't | |
448 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | |
2e04ef76 RR |
449 | * letting them do it. |
450 | :*/ | |
d7e28ffe RR |
451 | static int close(struct inode *inode, struct file *file) |
452 | { | |
453 | struct lguest *lg = file->private_data; | |
ad8d8f3b | 454 | unsigned int i; |
d7e28ffe | 455 | |
dde79789 | 456 | /* If we never successfully initialized, there's nothing to clean up */ |
d7e28ffe RR |
457 | if (!lg) |
458 | return 0; | |
459 | ||
2e04ef76 RR |
460 | /* |
461 | * We need the big lock, to protect from inter-guest I/O and other | |
462 | * Launchers initializing guests. | |
463 | */ | |
d7e28ffe | 464 | mutex_lock(&lguest_lock); |
66686c2a GOC |
465 | |
466 | /* Free up the shadow page tables for the Guest. */ | |
467 | free_guest_pagetable(lg); | |
468 | ||
a53a35a8 | 469 | for (i = 0; i < lg->nr_cpus; i++) { |
ad8d8f3b GOC |
470 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
471 | hrtimer_cancel(&lg->cpus[i].hrt); | |
a53a35a8 GOC |
472 | /* We can free up the register page we allocated. */ |
473 | free_page(lg->cpus[i].regs_page); | |
2e04ef76 RR |
474 | /* |
475 | * Now all the memory cleanups are done, it's safe to release | |
476 | * the Launcher's memory management structure. | |
477 | */ | |
66686c2a | 478 | mmput(lg->cpus[i].mm); |
a53a35a8 | 479 | } |
df60aeef RR |
480 | |
481 | /* Release any eventfds they registered. */ | |
482 | for (i = 0; i < lg->eventfds->num; i++) | |
13389010 | 483 | eventfd_ctx_put(lg->eventfds->map[i].event); |
df60aeef RR |
484 | kfree(lg->eventfds); |
485 | ||
2e04ef76 RR |
486 | /* |
487 | * If lg->dead doesn't contain an error code it will be NULL or a | |
488 | * kmalloc()ed string, either of which is ok to hand to kfree(). | |
489 | */ | |
d7e28ffe RR |
490 | if (!IS_ERR(lg->dead)) |
491 | kfree(lg->dead); | |
05dfdbbd MW |
492 | /* Free the memory allocated to the lguest_struct */ |
493 | kfree(lg); | |
dde79789 | 494 | /* Release lock and exit. */ |
d7e28ffe | 495 | mutex_unlock(&lguest_lock); |
dde79789 | 496 | |
d7e28ffe RR |
497 | return 0; |
498 | } | |
499 | ||
dde79789 RR |
500 | /*L:000 |
501 | * Welcome to our journey through the Launcher! | |
502 | * | |
503 | * The Launcher is the Host userspace program which sets up, runs and services | |
504 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" | |
505 | * doing things are inaccurate: the Launcher does all the device handling for | |
e1e72965 | 506 | * the Guest, but the Guest can't know that. |
dde79789 RR |
507 | * |
508 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we | |
509 | * shall see more of that later. | |
510 | * | |
511 | * We begin our understanding with the Host kernel interface which the Launcher | |
512 | * uses: reading and writing a character device called /dev/lguest. All the | |
2e04ef76 RR |
513 | * work happens in the read(), write() and close() routines: |
514 | */ | |
828c0950 | 515 | static const struct file_operations lguest_fops = { |
d7e28ffe RR |
516 | .owner = THIS_MODULE, |
517 | .release = close, | |
518 | .write = write, | |
519 | .read = read, | |
6038f373 | 520 | .llseek = default_llseek, |
d7e28ffe | 521 | }; |
9f54288d | 522 | /*:*/ |
dde79789 | 523 | |
2e04ef76 RR |
524 | /* |
525 | * This is a textbook example of a "misc" character device. Populate a "struct | |
526 | * miscdevice" and register it with misc_register(). | |
527 | */ | |
d7e28ffe RR |
528 | static struct miscdevice lguest_dev = { |
529 | .minor = MISC_DYNAMIC_MINOR, | |
530 | .name = "lguest", | |
531 | .fops = &lguest_fops, | |
532 | }; | |
533 | ||
534 | int __init lguest_device_init(void) | |
535 | { | |
536 | return misc_register(&lguest_dev); | |
537 | } | |
538 | ||
539 | void __exit lguest_device_remove(void) | |
540 | { | |
541 | misc_deregister(&lguest_dev); | |
542 | } |