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 accessing a device.
7 #include <linux/uaccess.h>
8 #include <linux/miscdevice.h>
10 #include <linux/sched.h>
11 #include <linux/sched/mm.h>
12 #include <linux/file.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
18 The Launcher can get the registers, and also set some of them.
20 static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input)
24 /* We re-use the ptrace structure to specify which register to read. */
25 if (get_user(which, input) != 0)
29 * We set up the cpu register pointer, and their next read will
30 * actually get the value (instead of running the guest).
32 * The last argument 'true' says we can access any register.
34 cpu->reg_read = lguest_arch_regptr(cpu, which, true);
38 /* And because this is a write() call, we return the length used. */
39 return sizeof(unsigned long) * 2;
42 static int setreg(struct lg_cpu *cpu, const unsigned long __user *input)
44 unsigned long which, value, *reg;
46 /* We re-use the ptrace structure to specify which register to read. */
47 if (get_user(which, input) != 0)
50 if (get_user(value, input) != 0)
53 /* The last argument 'false' means we can't access all registers. */
54 reg = lguest_arch_regptr(cpu, which, false);
60 /* And because this is a write() call, we return the length used. */
61 return sizeof(unsigned long) * 3;
65 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
66 * number to /dev/lguest.
68 static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
72 if (get_user(irq, input) != 0)
74 if (irq >= LGUEST_IRQS)
78 * Next time the Guest runs, the core code will see if it can deliver
81 set_interrupt(cpu, irq);
86 * Deliver a trap: this is used by the Launcher if it can't emulate
89 static int trap(struct lg_cpu *cpu, const unsigned long __user *input)
91 unsigned long trapnum;
93 if (get_user(trapnum, input) != 0)
96 if (!deliver_trap(cpu, trapnum))
103 * Once our Guest is initialized, the Launcher makes it run by reading
106 static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
108 struct lguest *lg = file->private_data;
110 unsigned int cpu_id = *o;
112 /* You must write LHREQ_INITIALIZE first! */
116 /* Watch out for arbitrary vcpu indexes! */
117 if (cpu_id >= lg->nr_cpus)
120 cpu = &lg->cpus[cpu_id];
122 /* If you're not the task which owns the Guest, go away. */
123 if (current != cpu->tsk)
126 /* If the Guest is already dead, we indicate why */
130 /* lg->dead either contains an error code, or a string. */
131 if (IS_ERR(lg->dead))
132 return PTR_ERR(lg->dead);
134 /* We can only return as much as the buffer they read with. */
135 len = min(size, strlen(lg->dead)+1);
136 if (copy_to_user(user, lg->dead, len) != 0)
142 * If we returned from read() last time because the Guest sent I/O,
145 if (cpu->pending.trap)
146 cpu->pending.trap = 0;
148 /* Run the Guest until something interesting happens. */
149 return run_guest(cpu, (unsigned long __user *)user);
153 * This actually initializes a CPU. For the moment, a Guest is only
154 * uniprocessor, so "id" is always 0.
156 static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
158 /* We have a limited number of CPUs in the lguest struct. */
159 if (id >= ARRAY_SIZE(cpu->lg->cpus))
162 /* Set up this CPU's id, and pointer back to the lguest struct. */
164 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
167 /* Each CPU has a timer it can set. */
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.
174 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
178 /* We actually put the registers at the end of the page. */
179 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
182 * Now we initialize the Guest's registers, handing it the start
185 lguest_arch_setup_regs(cpu, start_ip);
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).
194 * We need to keep a pointer to the Launcher's memory map, because if
195 * the Launcher dies we need to clean it up. If we don't keep a
196 * reference, it is destroyed before close() is called.
198 cpu->mm = get_task_mm(cpu->tsk);
201 * We remember which CPU's pages this Guest used last, for optimization
202 * when the same Guest runs on the same CPU twice.
204 cpu->last_pages = NULL;
206 /* No error == success. */
211 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
212 * addition to the LHREQ_INITIALIZE value). These are:
214 * base: The start of the Guest-physical memory inside the Launcher memory.
216 * pfnlimit: The highest (Guest-physical) page number the Guest should be
217 * 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.
220 * start: The first instruction to execute ("eip" in x86-speak).
222 static int initialize(struct file *file, const unsigned long __user *input)
224 /* "struct lguest" contains all we (the Host) know about a Guest. */
227 unsigned long args[4];
230 * We grab the Big Lguest lock, which protects against multiple
231 * simultaneous initializations.
233 mutex_lock(&lguest_lock);
234 /* You can't initialize twice! Close the device and start again... */
235 if (file->private_data) {
240 if (copy_from_user(args, input, sizeof(args)) != 0) {
245 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
251 /* Populate the easy fields of our "struct lguest" */
252 lg->mem_base = (void __user *)args[0];
253 lg->pfn_limit = args[1];
254 lg->device_limit = args[3];
256 /* 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]);
262 * Initialize the Guest's shadow page tables. This allocates
263 * memory, so can fail.
265 err = init_guest_pagetable(lg);
269 /* We keep our "struct lguest" in the file's private_data. */
270 file->private_data = lg;
272 mutex_unlock(&lguest_lock);
274 /* And because this is a write() call, we return the length used. */
278 /* FIXME: This should be in free_vcpu */
279 free_page(lg->cpus[0].regs_page);
283 mutex_unlock(&lguest_lock);
288 * The first operation the Launcher does must be a write. All writes
289 * start with an unsigned long number: for the first write this must be
290 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
291 * writes of other values to send interrupts or set up receipt of notifications.
293 * Note that we overload the "offset" in the /dev/lguest file to indicate what
294 * CPU number we're dealing with. Currently this is always 0 since we only
295 * support uniprocessor Guests, but you can see the beginnings of SMP support
298 static ssize_t write(struct file *file, const char __user *in,
299 size_t size, loff_t *off)
302 * Once the Guest is initialized, we hold the "struct lguest" in the
305 struct lguest *lg = file->private_data;
306 const unsigned long __user *input = (const unsigned long __user *)in;
308 struct lg_cpu *uninitialized_var(cpu);
309 unsigned int cpu_id = *off;
311 /* The first value tells us what this request is. */
312 if (get_user(req, input) != 0)
316 /* If you haven't initialized, you must do that first. */
317 if (req != LHREQ_INITIALIZE) {
318 if (!lg || (cpu_id >= lg->nr_cpus))
320 cpu = &lg->cpus[cpu_id];
322 /* Once the Guest is dead, you can only read() why it died. */
328 case LHREQ_INITIALIZE:
329 return initialize(file, input);
331 return user_send_irq(cpu, input);
333 return getreg_setup(cpu, input);
335 return setreg(cpu, input);
337 return trap(cpu, input);
343 static int open(struct inode *inode, struct file *file)
345 file->private_data = NULL;
351 * The final piece of interface code is the close() routine. It reverses
352 * everything done in initialize(). This is usually called because the
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
357 * letting them do it.
359 static int close(struct inode *inode, struct file *file)
361 struct lguest *lg = file->private_data;
364 /* If we never successfully initialized, there's nothing to clean up */
369 * We need the big lock, to protect from inter-guest I/O and other
370 * Launchers initializing guests.
372 mutex_lock(&lguest_lock);
374 /* Free up the shadow page tables for the Guest. */
375 free_guest_pagetable(lg);
377 for (i = 0; i < lg->nr_cpus; i++) {
378 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
379 hrtimer_cancel(&lg->cpus[i].hrt);
380 /* We can free up the register page we allocated. */
381 free_page(lg->cpus[i].regs_page);
383 * Now all the memory cleanups are done, it's safe to release
384 * the Launcher's memory management structure.
386 mmput(lg->cpus[i].mm);
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().
393 if (!IS_ERR(lg->dead))
395 /* Free the memory allocated to the lguest_struct */
397 /* Release lock and exit. */
398 mutex_unlock(&lguest_lock);
404 * Welcome to our journey through the Launcher!
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
409 * the Guest, but the Guest can't know that.
411 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
412 * shall see more of that later.
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
416 * work happens in the read(), write() and close() routines:
418 static const struct file_operations lguest_fops = {
419 .owner = THIS_MODULE,
424 .llseek = default_llseek,
429 * This is a textbook example of a "misc" character device. Populate a "struct
430 * miscdevice" and register it with misc_register().
432 static struct miscdevice lguest_dev = {
433 .minor = MISC_DYNAMIC_MINOR,
435 .fops = &lguest_fops,
438 int __init lguest_device_init(void)
440 return misc_register(&lguest_dev);
443 void __exit lguest_device_remove(void)
445 misc_deregister(&lguest_dev);