blob: 1a6787bc9386a67a57c4c42fc5a00d241f5bcda1 [file] [log] [blame]
Rusty Russell9f542882011-07-22 14:39:50 +09301/*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
Rusty Russelld9bab502015-02-11 15:28:01 +10305 * a signal or the Guest accessing a device.
Rusty Russell2e04ef72009-07-30 16:03:45 -06006:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07007#include <linux/uaccess.h>
8#include <linux/miscdevice.h>
9#include <linux/fs.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020010#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010011#include <linux/sched/mm.h>
Rusty Russelldf60aee2009-06-12 22:27:09 -060012#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Paul Gortmaker39a0e332011-07-21 13:03:20 -040014#include <linux/export.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070015#include "lg.h"
16
Rusty Russella91d74a2009-07-30 16:03:45 -060017/*L:052
Rusty Russelld9bab502015-02-11 15:28:01 +103018 The Launcher can get the registers, and also set some of them.
19*/
Rusty Russell18c13732015-02-11 15:15:09 +103020static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input)
21{
22 unsigned long which;
23
24 /* We re-use the ptrace structure to specify which register to read. */
25 if (get_user(which, input) != 0)
26 return -EFAULT;
27
28 /*
29 * We set up the cpu register pointer, and their next read will
30 * actually get the value (instead of running the guest).
31 *
32 * The last argument 'true' says we can access any register.
33 */
34 cpu->reg_read = lguest_arch_regptr(cpu, which, true);
35 if (!cpu->reg_read)
36 return -ENOENT;
37
38 /* And because this is a write() call, we return the length used. */
39 return sizeof(unsigned long) * 2;
40}
41
42static int setreg(struct lg_cpu *cpu, const unsigned long __user *input)
43{
44 unsigned long which, value, *reg;
45
46 /* We re-use the ptrace structure to specify which register to read. */
47 if (get_user(which, input) != 0)
48 return -EFAULT;
49 input++;
50 if (get_user(value, input) != 0)
51 return -EFAULT;
52
53 /* The last argument 'false' means we can't access all registers. */
54 reg = lguest_arch_regptr(cpu, which, false);
55 if (!reg)
56 return -ENOENT;
57
58 *reg = value;
59
60 /* And because this is a write() call, we return the length used. */
61 return sizeof(unsigned long) * 3;
62}
63
Rusty Russell2e04ef72009-07-30 16:03:45 -060064/*L:050
65 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
66 * number to /dev/lguest.
67 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020068static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070069{
Jes Sorensen511801d2007-10-22 11:03:31 +100070 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070071
72 if (get_user(irq, input) != 0)
73 return -EFAULT;
74 if (irq >= LGUEST_IRQS)
75 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -060076
Rusty Russella91d74a2009-07-30 16:03:45 -060077 /*
78 * Next time the Guest runs, the core code will see if it can deliver
79 * this interrupt.
80 */
Rusty Russell9f155a92009-06-12 22:27:08 -060081 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070082 return 0;
83}
84
Rusty Russell8ed31302015-02-11 15:15:09 +103085/*L:053
86 * Deliver a trap: this is used by the Launcher if it can't emulate
87 * an instruction.
88 */
89static int trap(struct lg_cpu *cpu, const unsigned long __user *input)
90{
91 unsigned long trapnum;
92
93 if (get_user(trapnum, input) != 0)
94 return -EFAULT;
95
96 if (!deliver_trap(cpu, trapnum))
97 return -EINVAL;
98
99 return 0;
100}
101
Rusty Russell2e04ef72009-07-30 16:03:45 -0600102/*L:040
103 * Once our Guest is initialized, the Launcher makes it run by reading
104 * from /dev/lguest.
105 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700106static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
107{
108 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200109 struct lg_cpu *cpu;
110 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700111
Rusty Russelldde79782007-07-26 10:41:03 -0700112 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700113 if (!lg)
114 return -EINVAL;
115
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200116 /* Watch out for arbitrary vcpu indexes! */
117 if (cpu_id >= lg->nr_cpus)
118 return -EINVAL;
119
120 cpu = &lg->cpus[cpu_id];
121
Rusty Russelle1e72962007-10-25 15:02:50 +1000122 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200123 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124 return -EPERM;
125
Rusty Russella6bd8e12008-03-28 11:05:53 -0500126 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700127 if (lg->dead) {
128 size_t len;
129
Rusty Russelldde79782007-07-26 10:41:03 -0700130 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700131 if (IS_ERR(lg->dead))
132 return PTR_ERR(lg->dead);
133
Rusty Russelldde79782007-07-26 10:41:03 -0700134 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700135 len = min(size, strlen(lg->dead)+1);
136 if (copy_to_user(user, lg->dead, len) != 0)
137 return -EFAULT;
138 return len;
139 }
140
Rusty Russell2e04ef72009-07-30 16:03:45 -0600141 /*
142 * If we returned from read() last time because the Guest sent I/O,
143 * clear the flag.
144 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030145 if (cpu->pending.trap)
146 cpu->pending.trap = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700147
Rusty Russelldde79782007-07-26 10:41:03 -0700148 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200149 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700150}
151
Rusty Russell2e04ef72009-07-30 16:03:45 -0600152/*L:025
153 * This actually initializes a CPU. For the moment, a Guest is only
154 * uniprocessor, so "id" is always 0.
155 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200156static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
157{
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930158 /* We have a limited number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500159 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200160 return -EINVAL;
161
Rusty Russella6bd8e12008-03-28 11:05:53 -0500162 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200163 cpu->id = id;
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930164 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200165 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500166
167 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200168 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200169
Rusty Russell2e04ef72009-07-30 16:03:45 -0600170 /*
171 * We need a complete page for the Guest registers: they are accessible
172 * to the Guest and we can only grant it access to whole pages.
173 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200174 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
175 if (!cpu->regs_page)
176 return -ENOMEM;
177
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930178 /* We actually put the registers at the end of the page. */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200179 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
180
Rusty Russell2e04ef72009-07-30 16:03:45 -0600181 /*
182 * Now we initialize the Guest's registers, handing it the start
183 * address.
184 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200185 lguest_arch_setup_regs(cpu, start_ip);
186
Rusty Russell2e04ef72009-07-30 16:03:45 -0600187 /*
188 * We keep a pointer to the Launcher task (ie. current task) for when
189 * other Guests want to wake this one (eg. console input).
190 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200191 cpu->tsk = current;
192
Rusty Russell2e04ef72009-07-30 16:03:45 -0600193 /*
194 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200195 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600196 * reference, it is destroyed before close() is called.
197 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200198 cpu->mm = get_task_mm(cpu->tsk);
199
Rusty Russell2e04ef72009-07-30 16:03:45 -0600200 /*
201 * We remember which CPU's pages this Guest used last, for optimization
202 * when the same Guest runs on the same CPU twice.
203 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200204 cpu->last_pages = NULL;
205
Rusty Russella6bd8e12008-03-28 11:05:53 -0500206 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200207 return 0;
208}
209
Rusty Russell2e04ef72009-07-30 16:03:45 -0600210/*L:020
211 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
212 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700213 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000214 * base: The start of the Guest-physical memory inside the Launcher memory.
215 *
Rusty Russelldde79782007-07-26 10:41:03 -0700216 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000217 * allowed to access. The Guest memory lives inside the Launcher, so it sets
218 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700219 *
Rusty Russelldde79782007-07-26 10:41:03 -0700220 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700221 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000222static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700223{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600224 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700225 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000226 int err;
Rusty Russell7313d522015-02-11 15:15:10 +1030227 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228
Rusty Russell2e04ef72009-07-30 16:03:45 -0600229 /*
230 * We grab the Big Lguest lock, which protects against multiple
231 * simultaneous initializations.
232 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700233 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700234 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235 if (file->private_data) {
236 err = -EBUSY;
237 goto unlock;
238 }
239
240 if (copy_from_user(args, input, sizeof(args)) != 0) {
241 err = -EFAULT;
242 goto unlock;
243 }
244
Rusty Russell48245cc2007-10-22 11:03:27 +1000245 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
246 if (!lg) {
247 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700248 goto unlock;
249 }
Rusty Russelldde79782007-07-26 10:41:03 -0700250
251 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000252 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000253 lg->pfn_limit = args[1];
Rusty Russell7313d522015-02-11 15:15:10 +1030254 lg->device_limit = args[3];
Rusty Russelldde79782007-07-26 10:41:03 -0700255
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300256 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
257 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200258 if (err)
Rusty Russelld9bab502015-02-11 15:28:01 +1030259 goto free_lg;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200260
Rusty Russell2e04ef72009-07-30 16:03:45 -0600261 /*
Rusty Russell9f542882011-07-22 14:39:50 +0930262 * Initialize the Guest's shadow page tables. This allocates
263 * memory, so can fail.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600264 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300265 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700266 if (err)
267 goto free_regs;
268
Rusty Russelldde79782007-07-26 10:41:03 -0700269 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700270 file->private_data = lg;
271
272 mutex_unlock(&lguest_lock);
273
Rusty Russelldde79782007-07-26 10:41:03 -0700274 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275 return sizeof(args);
276
277free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200278 /* FIXME: This should be in free_vcpu */
279 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600280free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800281 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700282unlock:
283 mutex_unlock(&lguest_lock);
284 return err;
285}
286
Rusty Russell2e04ef72009-07-30 16:03:45 -0600287/*L:010
288 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000289 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700290 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600291 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500292 *
293 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600294 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500295 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600296 * here.
297 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000298static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700299 size_t size, loff_t *off)
300{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600301 /*
302 * Once the Guest is initialized, we hold the "struct lguest" in the
303 * file private data.
304 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700305 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000306 const unsigned long __user *input = (const unsigned long __user *)in;
307 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200308 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200309 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700310
Rusty Russella6bd8e12008-03-28 11:05:53 -0500311 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700312 if (get_user(req, input) != 0)
313 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000314 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700315
Rusty Russelldde79782007-07-26 10:41:03 -0700316 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200317 if (req != LHREQ_INITIALIZE) {
318 if (!lg || (cpu_id >= lg->nr_cpus))
319 return -EINVAL;
320 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800321
322 /* Once the Guest is dead, you can only read() why it died. */
323 if (lg->dead)
324 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200325 }
Rusty Russelldde79782007-07-26 10:41:03 -0700326
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700327 switch (req) {
328 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000329 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700330 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200331 return user_send_irq(cpu, input);
Rusty Russell18c13732015-02-11 15:15:09 +1030332 case LHREQ_GETREG:
333 return getreg_setup(cpu, input);
334 case LHREQ_SETREG:
335 return setreg(cpu, input);
Rusty Russell8ed31302015-02-11 15:15:09 +1030336 case LHREQ_TRAP:
337 return trap(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700338 default:
339 return -EINVAL;
340 }
341}
342
Martin Kepplinger67c50bf2015-03-24 11:51:20 +1030343static int open(struct inode *inode, struct file *file)
344{
345 file->private_data = NULL;
346
347 return 0;
348}
349
Rusty Russell2e04ef72009-07-30 16:03:45 -0600350/*L:060
351 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700352 * everything done in initialize(). This is usually called because the
353 * Launcher exited.
354 *
355 * Note that the close routine returns 0 or a negative error number: it can't
356 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600357 * letting them do it.
358:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700359static int close(struct inode *inode, struct file *file)
360{
361 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200362 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700363
Rusty Russelldde79782007-07-26 10:41:03 -0700364 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700365 if (!lg)
366 return 0;
367
Rusty Russell2e04ef72009-07-30 16:03:45 -0600368 /*
369 * We need the big lock, to protect from inter-guest I/O and other
370 * Launchers initializing guests.
371 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700372 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200373
374 /* Free up the shadow page tables for the Guest. */
375 free_guest_pagetable(lg);
376
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200377 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200378 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
379 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200380 /* We can free up the register page we allocated. */
381 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600382 /*
383 * Now all the memory cleanups are done, it's safe to release
384 * the Launcher's memory management structure.
385 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200386 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200387 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600388
Rusty Russell2e04ef72009-07-30 16:03:45 -0600389 /*
390 * If lg->dead doesn't contain an error code it will be NULL or a
391 * kmalloc()ed string, either of which is ok to hand to kfree().
392 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700393 if (!IS_ERR(lg->dead))
394 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100395 /* Free the memory allocated to the lguest_struct */
396 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700397 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700398 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700399
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700400 return 0;
401}
402
Rusty Russelldde79782007-07-26 10:41:03 -0700403/*L:000
404 * Welcome to our journey through the Launcher!
405 *
406 * The Launcher is the Host userspace program which sets up, runs and services
407 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
408 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000409 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700410 *
411 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
412 * shall see more of that later.
413 *
414 * We begin our understanding with the Host kernel interface which the Launcher
415 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600416 * work happens in the read(), write() and close() routines:
417 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700418static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700419 .owner = THIS_MODULE,
Martin Kepplinger67c50bf2015-03-24 11:51:20 +1030420 .open = open,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700421 .release = close,
422 .write = write,
423 .read = read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200424 .llseek = default_llseek,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700425};
Rusty Russell9f542882011-07-22 14:39:50 +0930426/*:*/
Rusty Russelldde79782007-07-26 10:41:03 -0700427
Rusty Russell2e04ef72009-07-30 16:03:45 -0600428/*
429 * This is a textbook example of a "misc" character device. Populate a "struct
430 * miscdevice" and register it with misc_register().
431 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700432static struct miscdevice lguest_dev = {
433 .minor = MISC_DYNAMIC_MINOR,
434 .name = "lguest",
435 .fops = &lguest_fops,
436};
437
438int __init lguest_device_init(void)
439{
440 return misc_register(&lguest_dev);
441}
442
443void __exit lguest_device_remove(void)
444{
445 misc_deregister(&lguest_dev);
446}