blob: 30c60687d277c47efb20ea0a1bba4826444af09d [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>
Rusty Russelldf60aee2009-06-12 22:27:09 -060011#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Paul Gortmaker39a0e332011-07-21 13:03:20 -040013#include <linux/export.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070014#include "lg.h"
15
Rusty Russella91d74a2009-07-30 16:03:45 -060016/*L:052
Rusty Russelld9bab502015-02-11 15:28:01 +103017 The Launcher can get the registers, and also set some of them.
18*/
Rusty Russell18c13732015-02-11 15:15:09 +103019static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input)
20{
21 unsigned long which;
22
23 /* We re-use the ptrace structure to specify which register to read. */
24 if (get_user(which, input) != 0)
25 return -EFAULT;
26
27 /*
28 * We set up the cpu register pointer, and their next read will
29 * actually get the value (instead of running the guest).
30 *
31 * The last argument 'true' says we can access any register.
32 */
33 cpu->reg_read = lguest_arch_regptr(cpu, which, true);
34 if (!cpu->reg_read)
35 return -ENOENT;
36
37 /* And because this is a write() call, we return the length used. */
38 return sizeof(unsigned long) * 2;
39}
40
41static int setreg(struct lg_cpu *cpu, const unsigned long __user *input)
42{
43 unsigned long which, value, *reg;
44
45 /* We re-use the ptrace structure to specify which register to read. */
46 if (get_user(which, input) != 0)
47 return -EFAULT;
48 input++;
49 if (get_user(value, input) != 0)
50 return -EFAULT;
51
52 /* The last argument 'false' means we can't access all registers. */
53 reg = lguest_arch_regptr(cpu, which, false);
54 if (!reg)
55 return -ENOENT;
56
57 *reg = value;
58
59 /* And because this is a write() call, we return the length used. */
60 return sizeof(unsigned long) * 3;
61}
62
Rusty Russell2e04ef72009-07-30 16:03:45 -060063/*L:050
64 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
65 * number to /dev/lguest.
66 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020067static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070068{
Jes Sorensen511801d2007-10-22 11:03:31 +100069 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070070
71 if (get_user(irq, input) != 0)
72 return -EFAULT;
73 if (irq >= LGUEST_IRQS)
74 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -060075
Rusty Russella91d74a2009-07-30 16:03:45 -060076 /*
77 * Next time the Guest runs, the core code will see if it can deliver
78 * this interrupt.
79 */
Rusty Russell9f155a92009-06-12 22:27:08 -060080 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070081 return 0;
82}
83
Rusty Russell8ed31302015-02-11 15:15:09 +103084/*L:053
85 * Deliver a trap: this is used by the Launcher if it can't emulate
86 * an instruction.
87 */
88static int trap(struct lg_cpu *cpu, const unsigned long __user *input)
89{
90 unsigned long trapnum;
91
92 if (get_user(trapnum, input) != 0)
93 return -EFAULT;
94
95 if (!deliver_trap(cpu, trapnum))
96 return -EINVAL;
97
98 return 0;
99}
100
Rusty Russell2e04ef72009-07-30 16:03:45 -0600101/*L:040
102 * Once our Guest is initialized, the Launcher makes it run by reading
103 * from /dev/lguest.
104 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700105static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
106{
107 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200108 struct lg_cpu *cpu;
109 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700110
Rusty Russelldde79782007-07-26 10:41:03 -0700111 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700112 if (!lg)
113 return -EINVAL;
114
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200115 /* Watch out for arbitrary vcpu indexes! */
116 if (cpu_id >= lg->nr_cpus)
117 return -EINVAL;
118
119 cpu = &lg->cpus[cpu_id];
120
Rusty Russelle1e72962007-10-25 15:02:50 +1000121 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200122 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700123 return -EPERM;
124
Rusty Russella6bd8e12008-03-28 11:05:53 -0500125 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700126 if (lg->dead) {
127 size_t len;
128
Rusty Russelldde79782007-07-26 10:41:03 -0700129 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700130 if (IS_ERR(lg->dead))
131 return PTR_ERR(lg->dead);
132
Rusty Russelldde79782007-07-26 10:41:03 -0700133 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700134 len = min(size, strlen(lg->dead)+1);
135 if (copy_to_user(user, lg->dead, len) != 0)
136 return -EFAULT;
137 return len;
138 }
139
Rusty Russell2e04ef72009-07-30 16:03:45 -0600140 /*
141 * If we returned from read() last time because the Guest sent I/O,
142 * clear the flag.
143 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030144 if (cpu->pending.trap)
145 cpu->pending.trap = 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 Russell2e04ef72009-07-30 16:03:45 -0600151/*L:025
152 * This actually initializes a CPU. For the moment, a Guest is only
153 * uniprocessor, so "id" is always 0.
154 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200155static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
156{
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930157 /* We have a limited number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500158 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200159 return -EINVAL;
160
Rusty Russella6bd8e12008-03-28 11:05:53 -0500161 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200162 cpu->id = id;
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930163 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200164 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500165
166 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200167 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200168
Rusty Russell2e04ef72009-07-30 16:03:45 -0600169 /*
170 * We need a complete page for the Guest registers: they are accessible
171 * to the Guest and we can only grant it access to whole pages.
172 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200173 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
174 if (!cpu->regs_page)
175 return -ENOMEM;
176
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930177 /* We actually put the registers at the end of the page. */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200178 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
179
Rusty Russell2e04ef72009-07-30 16:03:45 -0600180 /*
181 * Now we initialize the Guest's registers, handing it the start
182 * address.
183 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200184 lguest_arch_setup_regs(cpu, start_ip);
185
Rusty Russell2e04ef72009-07-30 16:03:45 -0600186 /*
187 * We keep a pointer to the Launcher task (ie. current task) for when
188 * other Guests want to wake this one (eg. console input).
189 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200190 cpu->tsk = current;
191
Rusty Russell2e04ef72009-07-30 16:03:45 -0600192 /*
193 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200194 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600195 * reference, it is destroyed before close() is called.
196 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200197 cpu->mm = get_task_mm(cpu->tsk);
198
Rusty Russell2e04ef72009-07-30 16:03:45 -0600199 /*
200 * We remember which CPU's pages this Guest used last, for optimization
201 * when the same Guest runs on the same CPU twice.
202 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200203 cpu->last_pages = NULL;
204
Rusty Russella6bd8e12008-03-28 11:05:53 -0500205 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200206 return 0;
207}
208
Rusty Russell2e04ef72009-07-30 16:03:45 -0600209/*L:020
210 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
211 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700212 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000213 * base: The start of the Guest-physical memory inside the Launcher memory.
214 *
Rusty Russelldde79782007-07-26 10:41:03 -0700215 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000216 * allowed to access. The Guest memory lives inside the Launcher, so it sets
217 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700218 *
Rusty Russelldde79782007-07-26 10:41:03 -0700219 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700220 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000221static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700222{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600223 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700224 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000225 int err;
Rusty Russell7313d522015-02-11 15:15:10 +1030226 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700227
Rusty Russell2e04ef72009-07-30 16:03:45 -0600228 /*
229 * We grab the Big Lguest lock, which protects against multiple
230 * simultaneous initializations.
231 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700232 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700233 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700234 if (file->private_data) {
235 err = -EBUSY;
236 goto unlock;
237 }
238
239 if (copy_from_user(args, input, sizeof(args)) != 0) {
240 err = -EFAULT;
241 goto unlock;
242 }
243
Rusty Russell48245cc2007-10-22 11:03:27 +1000244 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
245 if (!lg) {
246 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700247 goto unlock;
248 }
Rusty Russelldde79782007-07-26 10:41:03 -0700249
250 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000251 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000252 lg->pfn_limit = args[1];
Rusty Russell7313d522015-02-11 15:15:10 +1030253 lg->device_limit = args[3];
Rusty Russelldde79782007-07-26 10:41:03 -0700254
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300255 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
256 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200257 if (err)
Rusty Russelld9bab502015-02-11 15:28:01 +1030258 goto free_lg;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200259
Rusty Russell2e04ef72009-07-30 16:03:45 -0600260 /*
Rusty Russell9f542882011-07-22 14:39:50 +0930261 * Initialize the Guest's shadow page tables. This allocates
262 * memory, so can fail.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600263 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300264 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700265 if (err)
266 goto free_regs;
267
Rusty Russelldde79782007-07-26 10:41:03 -0700268 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700269 file->private_data = lg;
270
271 mutex_unlock(&lguest_lock);
272
Rusty Russelldde79782007-07-26 10:41:03 -0700273 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700274 return sizeof(args);
275
276free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200277 /* FIXME: This should be in free_vcpu */
278 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600279free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800280 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700281unlock:
282 mutex_unlock(&lguest_lock);
283 return err;
284}
285
Rusty Russell2e04ef72009-07-30 16:03:45 -0600286/*L:010
287 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000288 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700289 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600290 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500291 *
292 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600293 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500294 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600295 * here.
296 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000297static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700298 size_t size, loff_t *off)
299{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600300 /*
301 * Once the Guest is initialized, we hold the "struct lguest" in the
302 * file private data.
303 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700304 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000305 const unsigned long __user *input = (const unsigned long __user *)in;
306 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200307 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200308 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700309
Rusty Russella6bd8e12008-03-28 11:05:53 -0500310 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700311 if (get_user(req, input) != 0)
312 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000313 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700314
Rusty Russelldde79782007-07-26 10:41:03 -0700315 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200316 if (req != LHREQ_INITIALIZE) {
317 if (!lg || (cpu_id >= lg->nr_cpus))
318 return -EINVAL;
319 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800320
321 /* Once the Guest is dead, you can only read() why it died. */
322 if (lg->dead)
323 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200324 }
Rusty Russelldde79782007-07-26 10:41:03 -0700325
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700326 switch (req) {
327 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000328 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700329 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200330 return user_send_irq(cpu, input);
Rusty Russell18c13732015-02-11 15:15:09 +1030331 case LHREQ_GETREG:
332 return getreg_setup(cpu, input);
333 case LHREQ_SETREG:
334 return setreg(cpu, input);
Rusty Russell8ed31302015-02-11 15:15:09 +1030335 case LHREQ_TRAP:
336 return trap(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700337 default:
338 return -EINVAL;
339 }
340}
341
Martin Kepplinger67c50bf2015-03-24 11:51:20 +1030342static int open(struct inode *inode, struct file *file)
343{
344 file->private_data = NULL;
345
346 return 0;
347}
348
Rusty Russell2e04ef72009-07-30 16:03:45 -0600349/*L:060
350 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700351 * everything done in initialize(). This is usually called because the
352 * Launcher exited.
353 *
354 * Note that the close routine returns 0 or a negative error number: it can't
355 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600356 * letting them do it.
357:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700358static int close(struct inode *inode, struct file *file)
359{
360 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200361 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700362
Rusty Russelldde79782007-07-26 10:41:03 -0700363 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700364 if (!lg)
365 return 0;
366
Rusty Russell2e04ef72009-07-30 16:03:45 -0600367 /*
368 * We need the big lock, to protect from inter-guest I/O and other
369 * Launchers initializing guests.
370 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700371 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200372
373 /* Free up the shadow page tables for the Guest. */
374 free_guest_pagetable(lg);
375
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200376 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200377 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
378 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200379 /* We can free up the register page we allocated. */
380 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600381 /*
382 * Now all the memory cleanups are done, it's safe to release
383 * the Launcher's memory management structure.
384 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200385 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200386 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600387
Rusty Russell2e04ef72009-07-30 16:03:45 -0600388 /*
389 * If lg->dead doesn't contain an error code it will be NULL or a
390 * kmalloc()ed string, either of which is ok to hand to kfree().
391 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700392 if (!IS_ERR(lg->dead))
393 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100394 /* Free the memory allocated to the lguest_struct */
395 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700396 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700397 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700398
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700399 return 0;
400}
401
Rusty Russelldde79782007-07-26 10:41:03 -0700402/*L:000
403 * Welcome to our journey through the Launcher!
404 *
405 * The Launcher is the Host userspace program which sets up, runs and services
406 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
407 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000408 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700409 *
410 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
411 * shall see more of that later.
412 *
413 * We begin our understanding with the Host kernel interface which the Launcher
414 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600415 * work happens in the read(), write() and close() routines:
416 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700417static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700418 .owner = THIS_MODULE,
Martin Kepplinger67c50bf2015-03-24 11:51:20 +1030419 .open = open,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700420 .release = close,
421 .write = write,
422 .read = read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200423 .llseek = default_llseek,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700424};
Rusty Russell9f542882011-07-22 14:39:50 +0930425/*:*/
Rusty Russelldde79782007-07-26 10:41:03 -0700426
Rusty Russell2e04ef72009-07-30 16:03:45 -0600427/*
428 * This is a textbook example of a "misc" character device. Populate a "struct
429 * miscdevice" and register it with misc_register().
430 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700431static struct miscdevice lguest_dev = {
432 .minor = MISC_DYNAMIC_MINOR,
433 .name = "lguest",
434 .fops = &lguest_fops,
435};
436
437int __init lguest_device_init(void)
438{
439 return misc_register(&lguest_dev);
440}
441
442void __exit lguest_device_remove(void)
443{
444 misc_deregister(&lguest_dev);
445}