blob: 407722a8e0c45868b04b009bcb25e01b0c07ed3a [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 Russelldf60aee2009-06-12 22:27:09 -060014bool send_notify_to_eventfd(struct lg_cpu *cpu)
15{
16 unsigned int i;
17 struct lg_eventfd_map *map;
18
19 /* lg->eventfds is RCU-protected */
20 rcu_read_lock();
21 map = rcu_dereference(cpu->lg->eventfds);
22 for (i = 0; i < map->num; i++) {
23 if (map->map[i].addr == cpu->pending_notify) {
24 eventfd_signal(map->map[i].event, 1);
25 cpu->pending_notify = 0;
26 break;
27 }
28 }
29 rcu_read_unlock();
30 return cpu->pending_notify == 0;
31}
32
33static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
34{
35 struct lg_eventfd_map *new, *old = lg->eventfds;
36
37 if (!addr)
38 return -EINVAL;
39
40 /* Replace the old array with the new one, carefully: others can
41 * be accessing it at the same time */
42 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
43 GFP_KERNEL);
44 if (!new)
45 return -ENOMEM;
46
47 /* First make identical copy. */
48 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
49 new->num = old->num;
50
51 /* Now append new entry. */
52 new->map[new->num].addr = addr;
Davide Libenzi13389012009-06-30 11:41:11 -070053 new->map[new->num].event = eventfd_ctx_fdget(fd);
Rusty Russelldf60aee2009-06-12 22:27:09 -060054 if (IS_ERR(new->map[new->num].event)) {
Dan Carpenterf2945262009-07-19 14:46:09 +030055 int err = PTR_ERR(new->map[new->num].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -060056 kfree(new);
Dan Carpenterf2945262009-07-19 14:46:09 +030057 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -060058 }
59 new->num++;
60
61 /* Now put new one in place. */
62 rcu_assign_pointer(lg->eventfds, new);
63
64 /* We're not in a big hurry. Wait until noone's looking at old
65 * version, then delete it. */
66 synchronize_rcu();
67 kfree(old);
68
69 return 0;
70}
71
72static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
73{
74 unsigned long addr, fd;
75 int err;
76
77 if (get_user(addr, input) != 0)
78 return -EFAULT;
79 input++;
80 if (get_user(fd, input) != 0)
81 return -EFAULT;
82
83 mutex_lock(&lguest_lock);
84 err = add_eventfd(lg, addr, fd);
85 mutex_unlock(&lguest_lock);
86
Dan Carpenterf2945262009-07-19 14:46:09 +030087 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -060088}
89
Rusty Russelldde79782007-07-26 10:41:03 -070090/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
91 * number to /dev/lguest. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020092static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070093{
Jes Sorensen511801d2007-10-22 11:03:31 +100094 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070095
96 if (get_user(irq, input) != 0)
97 return -EFAULT;
98 if (irq >= LGUEST_IRQS)
99 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600100
101 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700102 return 0;
103}
104
Rusty Russelldde79782007-07-26 10:41:03 -0700105/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
106 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700107static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
108{
109 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200110 struct lg_cpu *cpu;
111 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700112
Rusty Russelldde79782007-07-26 10:41:03 -0700113 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700114 if (!lg)
115 return -EINVAL;
116
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200117 /* Watch out for arbitrary vcpu indexes! */
118 if (cpu_id >= lg->nr_cpus)
119 return -EINVAL;
120
121 cpu = &lg->cpus[cpu_id];
122
Rusty Russelle1e72962007-10-25 15:02:50 +1000123 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200124 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700125 return -EPERM;
126
Rusty Russella6bd8e12008-03-28 11:05:53 -0500127 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700128 if (lg->dead) {
129 size_t len;
130
Rusty Russelldde79782007-07-26 10:41:03 -0700131 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700132 if (IS_ERR(lg->dead))
133 return PTR_ERR(lg->dead);
134
Rusty Russelldde79782007-07-26 10:41:03 -0700135 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700136 len = min(size, strlen(lg->dead)+1);
137 if (copy_to_user(user, lg->dead, len) != 0)
138 return -EFAULT;
139 return len;
140 }
141
Rusty Russella6bd8e12008-03-28 11:05:53 -0500142 /* If we returned from read() last time because the Guest sent I/O,
Rusty Russelldde79782007-07-26 10:41:03 -0700143 * clear the flag. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200144 if (cpu->pending_notify)
145 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700146
Rusty Russelldde79782007-07-26 10:41:03 -0700147 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200148 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700149}
150
Rusty Russella6bd8e12008-03-28 11:05:53 -0500151/*L:025 This actually initializes a CPU. For the moment, a Guest is only
152 * uniprocessor, so "id" is always 0. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200153static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
154{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500155 /* We have a limited number the number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500156 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200157 return -EINVAL;
158
Rusty Russella6bd8e12008-03-28 11:05:53 -0500159 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200160 cpu->id = id;
161 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
162 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500163
164 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200165 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200166
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200167 /* We need a complete page for the Guest registers: they are accessible
168 * to the Guest and we can only grant it access to whole pages. */
169 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
170 if (!cpu->regs_page)
171 return -ENOMEM;
172
173 /* We actually put the registers at the bottom of the page. */
174 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
175
176 /* Now we initialize the Guest's registers, handing it the start
177 * address. */
178 lguest_arch_setup_regs(cpu, start_ip);
179
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200180 /* We keep a pointer to the Launcher task (ie. current task) for when
Rusty Russella6bd8e12008-03-28 11:05:53 -0500181 * other Guests want to wake this one (eg. console input). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200182 cpu->tsk = current;
183
184 /* We need to keep a pointer to the Launcher's memory map, because if
185 * the Launcher dies we need to clean it up. If we don't keep a
186 * reference, it is destroyed before close() is called. */
187 cpu->mm = get_task_mm(cpu->tsk);
188
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200189 /* We remember which CPU's pages this Guest used last, for optimization
190 * when the same Guest runs on the same CPU twice. */
191 cpu->last_pages = NULL;
192
Rusty Russella6bd8e12008-03-28 11:05:53 -0500193 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200194 return 0;
195}
196
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300197/*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000198 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700199 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000200 * base: The start of the Guest-physical memory inside the Launcher memory.
201 *
Rusty Russelldde79782007-07-26 10:41:03 -0700202 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000203 * allowed to access. The Guest memory lives inside the Launcher, so it sets
204 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700205 *
Rusty Russelldde79782007-07-26 10:41:03 -0700206 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700207 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000208static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700209{
Rusty Russelldde79782007-07-26 10:41:03 -0700210 /* "struct lguest" contains everything we (the Host) know about a
211 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700212 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000213 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300214 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700215
Rusty Russell48245cc2007-10-22 11:03:27 +1000216 /* We grab the Big Lguest lock, which protects against multiple
217 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700218 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700219 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700220 if (file->private_data) {
221 err = -EBUSY;
222 goto unlock;
223 }
224
225 if (copy_from_user(args, input, sizeof(args)) != 0) {
226 err = -EFAULT;
227 goto unlock;
228 }
229
Rusty Russell48245cc2007-10-22 11:03:27 +1000230 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
231 if (!lg) {
232 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700233 goto unlock;
234 }
Rusty Russelldde79782007-07-26 10:41:03 -0700235
Rusty Russelldf60aee2009-06-12 22:27:09 -0600236 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
237 if (!lg->eventfds) {
238 err = -ENOMEM;
239 goto free_lg;
240 }
241 lg->eventfds->num = 0;
242
Rusty Russelldde79782007-07-26 10:41:03 -0700243 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000244 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000245 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700246
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300247 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
248 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200249 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600250 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200251
Rusty Russelldde79782007-07-26 10:41:03 -0700252 /* Initialize the Guest's shadow page tables, using the toplevel
Rusty Russella6bd8e12008-03-28 11:05:53 -0500253 * address the Launcher gave us. This allocates memory, so can fail. */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300254 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700255 if (err)
256 goto free_regs;
257
Rusty Russelldde79782007-07-26 10:41:03 -0700258 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700259 file->private_data = lg;
260
261 mutex_unlock(&lguest_lock);
262
Rusty Russelldde79782007-07-26 10:41:03 -0700263 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700264 return sizeof(args);
265
266free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200267 /* FIXME: This should be in free_vcpu */
268 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600269free_eventfds:
270 kfree(lg->eventfds);
271free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800272 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700273unlock:
274 mutex_unlock(&lguest_lock);
275 return err;
276}
277
Rusty Russelldde79782007-07-26 10:41:03 -0700278/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000279 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700280 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella6bd8e12008-03-28 11:05:53 -0500281 * writes of other values to send interrupts.
282 *
283 * Note that we overload the "offset" in the /dev/lguest file to indicate what
284 * CPU number we're dealing with. Currently this is always 0, since we only
285 * support uniprocessor Guests, but you can see the beginnings of SMP support
286 * here. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000287static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700288 size_t size, loff_t *off)
289{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500290 /* Once the Guest is initialized, we hold the "struct lguest" in the
Rusty Russelldde79782007-07-26 10:41:03 -0700291 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000293 const unsigned long __user *input = (const unsigned long __user *)in;
294 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200295 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200296 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700297
Rusty Russella6bd8e12008-03-28 11:05:53 -0500298 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700299 if (get_user(req, input) != 0)
300 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000301 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700302
Rusty Russelldde79782007-07-26 10:41:03 -0700303 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200304 if (req != LHREQ_INITIALIZE) {
305 if (!lg || (cpu_id >= lg->nr_cpus))
306 return -EINVAL;
307 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800308
309 /* Once the Guest is dead, you can only read() why it died. */
310 if (lg->dead)
311 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200312 }
Rusty Russelldde79782007-07-26 10:41:03 -0700313
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700314 switch (req) {
315 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000316 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700317 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200318 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600319 case LHREQ_EVENTFD:
320 return attach_eventfd(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700321 default:
322 return -EINVAL;
323 }
324}
325
Rusty Russelldde79782007-07-26 10:41:03 -0700326/*L:060 The final piece of interface code is the close() routine. It reverses
327 * everything done in initialize(). This is usually called because the
328 * Launcher exited.
329 *
330 * Note that the close routine returns 0 or a negative error number: it can't
331 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
332 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700333static int close(struct inode *inode, struct file *file)
334{
335 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200336 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700337
Rusty Russelldde79782007-07-26 10:41:03 -0700338 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700339 if (!lg)
340 return 0;
341
Rusty Russelldde79782007-07-26 10:41:03 -0700342 /* We need the big lock, to protect from inter-guest I/O and other
343 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700344 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200345
346 /* Free up the shadow page tables for the Guest. */
347 free_guest_pagetable(lg);
348
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200349 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200350 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
351 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200352 /* We can free up the register page we allocated. */
353 free_page(lg->cpus[i].regs_page);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200354 /* Now all the memory cleanups are done, it's safe to release
355 * the Launcher's memory management structure. */
356 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200357 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600358
359 /* Release any eventfds they registered. */
360 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700361 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600362 kfree(lg->eventfds);
363
Rusty Russelldde79782007-07-26 10:41:03 -0700364 /* If lg->dead doesn't contain an error code it will be NULL or a
365 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700366 if (!IS_ERR(lg->dead))
367 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100368 /* Free the memory allocated to the lguest_struct */
369 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700370 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700371 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700372
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700373 return 0;
374}
375
Rusty Russelldde79782007-07-26 10:41:03 -0700376/*L:000
377 * Welcome to our journey through the Launcher!
378 *
379 * The Launcher is the Host userspace program which sets up, runs and services
380 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
381 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000382 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700383 *
384 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
385 * shall see more of that later.
386 *
387 * We begin our understanding with the Host kernel interface which the Launcher
388 * uses: reading and writing a character device called /dev/lguest. All the
389 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700390static struct file_operations lguest_fops = {
391 .owner = THIS_MODULE,
392 .release = close,
393 .write = write,
394 .read = read,
395};
Rusty Russelldde79782007-07-26 10:41:03 -0700396
397/* This is a textbook example of a "misc" character device. Populate a "struct
398 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700399static struct miscdevice lguest_dev = {
400 .minor = MISC_DYNAMIC_MINOR,
401 .name = "lguest",
402 .fops = &lguest_fops,
403};
404
405int __init lguest_device_init(void)
406{
407 return misc_register(&lguest_dev);
408}
409
410void __exit lguest_device_remove(void)
411{
412 misc_deregister(&lguest_dev);
413}