blob: a87fca678c6b9a1160ffbd6e287ff30d3be772ed [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>
9#include "lg.h"
10
Rusty Russelle1e72962007-10-25 15:02:50 +100011/*L:055 When something happens, the Waker process needs a way to stop the
12 * kernel running the Guest and return to the Launcher. So the Waker writes
13 * LHREQ_BREAK and the value "1" to /dev/lguest to do this. Once the Launcher
14 * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
15 * the Waker. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020016static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070017{
18 unsigned long on;
19
Rusty Russelle1e72962007-10-25 15:02:50 +100020 /* Fetch whether they're turning break on or off. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070021 if (get_user(on, input) != 0)
22 return -EFAULT;
23
24 if (on) {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020025 cpu->break_out = 1;
Rusty Russelle1e72962007-10-25 15:02:50 +100026 /* Pop it out of the Guest (may be running on different CPU) */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020027 wake_up_process(cpu->tsk);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070028 /* Wait for them to reset it */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020029 return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070030 } else {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020031 cpu->break_out = 0;
32 wake_up(&cpu->break_wq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070033 return 0;
34 }
35}
36
Rusty Russelldde79782007-07-26 10:41:03 -070037/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
38 * number to /dev/lguest. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020039static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070040{
Jes Sorensen511801d2007-10-22 11:03:31 +100041 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070042
43 if (get_user(irq, input) != 0)
44 return -EFAULT;
45 if (irq >= LGUEST_IRQS)
46 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -070047 /* Next time the Guest runs, the core code will see if it can deliver
48 * this interrupt. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020049 set_bit(irq, cpu->irqs_pending);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070050 return 0;
51}
52
Rusty Russelldde79782007-07-26 10:41:03 -070053/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
54 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070055static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
56{
57 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020058 struct lg_cpu *cpu;
59 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070060
Rusty Russelldde79782007-07-26 10:41:03 -070061 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070062 if (!lg)
63 return -EINVAL;
64
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020065 /* Watch out for arbitrary vcpu indexes! */
66 if (cpu_id >= lg->nr_cpus)
67 return -EINVAL;
68
69 cpu = &lg->cpus[cpu_id];
70
Rusty Russelle1e72962007-10-25 15:02:50 +100071 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020072 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070073 return -EPERM;
74
Rusty Russelldde79782007-07-26 10:41:03 -070075 /* If the guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070076 if (lg->dead) {
77 size_t len;
78
Rusty Russelldde79782007-07-26 10:41:03 -070079 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070080 if (IS_ERR(lg->dead))
81 return PTR_ERR(lg->dead);
82
Rusty Russelldde79782007-07-26 10:41:03 -070083 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070084 len = min(size, strlen(lg->dead)+1);
85 if (copy_to_user(user, lg->dead, len) != 0)
86 return -EFAULT;
87 return len;
88 }
89
Rusty Russell15045272007-10-22 11:24:10 +100090 /* If we returned from read() last time because the Guest notified,
Rusty Russelldde79782007-07-26 10:41:03 -070091 * clear the flag. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -020092 if (cpu->pending_notify)
93 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070094
Rusty Russelldde79782007-07-26 10:41:03 -070095 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020096 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070097}
98
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -020099static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
100{
101 if (id >= NR_CPUS)
102 return -EINVAL;
103
104 cpu->id = id;
105 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
106 cpu->lg->nr_cpus++;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200107 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200108
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200109 /* We need a complete page for the Guest registers: they are accessible
110 * to the Guest and we can only grant it access to whole pages. */
111 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
112 if (!cpu->regs_page)
113 return -ENOMEM;
114
115 /* We actually put the registers at the bottom of the page. */
116 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
117
118 /* Now we initialize the Guest's registers, handing it the start
119 * address. */
120 lguest_arch_setup_regs(cpu, start_ip);
121
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200122 /* Initialize the queue for the waker to wait on */
123 init_waitqueue_head(&cpu->break_wq);
124
125 /* We keep a pointer to the Launcher task (ie. current task) for when
126 * other Guests want to wake this one (inter-Guest I/O). */
127 cpu->tsk = current;
128
129 /* We need to keep a pointer to the Launcher's memory map, because if
130 * the Launcher dies we need to clean it up. If we don't keep a
131 * reference, it is destroyed before close() is called. */
132 cpu->mm = get_task_mm(cpu->tsk);
133
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200134 /* We remember which CPU's pages this Guest used last, for optimization
135 * when the same Guest runs on the same CPU twice. */
136 cpu->last_pages = NULL;
137
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200138 return 0;
139}
140
Rusty Russell47436aa2007-10-22 11:03:36 +1000141/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000142 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700143 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000144 * base: The start of the Guest-physical memory inside the Launcher memory.
145 *
Rusty Russelldde79782007-07-26 10:41:03 -0700146 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000147 * allowed to access. The Guest memory lives inside the Launcher, so it sets
148 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700149 *
150 * pgdir: The (Guest-physical) address of the top of the initial Guest
151 * pagetables (which are set up by the Launcher).
152 *
153 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700154 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000155static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700156{
Rusty Russelldde79782007-07-26 10:41:03 -0700157 /* "struct lguest" contains everything we (the Host) know about a
158 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700159 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000160 int err;
Rusty Russell47436aa2007-10-22 11:03:36 +1000161 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700162
Rusty Russell48245cc2007-10-22 11:03:27 +1000163 /* We grab the Big Lguest lock, which protects against multiple
164 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700165 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700166 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700167 if (file->private_data) {
168 err = -EBUSY;
169 goto unlock;
170 }
171
172 if (copy_from_user(args, input, sizeof(args)) != 0) {
173 err = -EFAULT;
174 goto unlock;
175 }
176
Rusty Russell48245cc2007-10-22 11:03:27 +1000177 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
178 if (!lg) {
179 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700180 goto unlock;
181 }
Rusty Russelldde79782007-07-26 10:41:03 -0700182
183 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000184 lg->mem_base = (void __user *)(long)args[0];
185 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700186
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200187 /* This is the first cpu */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200188 err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200189 if (err)
190 goto release_guest;
191
Rusty Russelldde79782007-07-26 10:41:03 -0700192 /* Initialize the Guest's shadow page tables, using the toplevel
193 * address the Launcher gave us. This allocates memory, so can
194 * fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000195 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700196 if (err)
197 goto free_regs;
198
Rusty Russelldde79782007-07-26 10:41:03 -0700199 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700200 file->private_data = lg;
201
202 mutex_unlock(&lguest_lock);
203
Rusty Russelldde79782007-07-26 10:41:03 -0700204 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700205 return sizeof(args);
206
207free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200208 /* FIXME: This should be in free_vcpu */
209 free_page(lg->cpus[0].regs_page);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700210release_guest:
Adrian Bunk43054412007-11-14 16:59:00 -0800211 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700212unlock:
213 mutex_unlock(&lguest_lock);
214 return err;
215}
216
Rusty Russelldde79782007-07-26 10:41:03 -0700217/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000218 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700219 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russell15045272007-10-22 11:24:10 +1000220 * writes of other values to send interrupts. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000221static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700222 size_t size, loff_t *off)
223{
Rusty Russelldde79782007-07-26 10:41:03 -0700224 /* Once the guest is initialized, we hold the "struct lguest" in the
225 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700226 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000227 const unsigned long __user *input = (const unsigned long __user *)in;
228 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200229 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200230 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700231
232 if (get_user(req, input) != 0)
233 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000234 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235
Rusty Russelldde79782007-07-26 10:41:03 -0700236 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200237 if (req != LHREQ_INITIALIZE) {
238 if (!lg || (cpu_id >= lg->nr_cpus))
239 return -EINVAL;
240 cpu = &lg->cpus[cpu_id];
241 if (!cpu)
242 return -EINVAL;
243 }
Rusty Russelldde79782007-07-26 10:41:03 -0700244
245 /* Once the Guest is dead, all you can do is read() why it died. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700246 if (lg && lg->dead)
247 return -ENOENT;
248
249 /* If you're not the task which owns the Guest, you can only break */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200250 if (lg && current != cpu->tsk && req != LHREQ_BREAK)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700251 return -EPERM;
252
253 switch (req) {
254 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000255 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700256 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200257 return user_send_irq(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700258 case LHREQ_BREAK:
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200259 return break_guest_out(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700260 default:
261 return -EINVAL;
262 }
263}
264
Rusty Russelldde79782007-07-26 10:41:03 -0700265/*L:060 The final piece of interface code is the close() routine. It reverses
266 * everything done in initialize(). This is usually called because the
267 * Launcher exited.
268 *
269 * Note that the close routine returns 0 or a negative error number: it can't
270 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
271 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700272static int close(struct inode *inode, struct file *file)
273{
274 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200275 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700276
Rusty Russelldde79782007-07-26 10:41:03 -0700277 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700278 if (!lg)
279 return 0;
280
Rusty Russelldde79782007-07-26 10:41:03 -0700281 /* We need the big lock, to protect from inter-guest I/O and other
282 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700283 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200284
285 /* Free up the shadow page tables for the Guest. */
286 free_guest_pagetable(lg);
287
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200288 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200289 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
290 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200291 /* We can free up the register page we allocated. */
292 free_page(lg->cpus[i].regs_page);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200293 /* Now all the memory cleanups are done, it's safe to release
294 * the Launcher's memory management structure. */
295 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200296 }
Rusty Russelldde79782007-07-26 10:41:03 -0700297 /* If lg->dead doesn't contain an error code it will be NULL or a
298 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700299 if (!IS_ERR(lg->dead))
300 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700301 /* We clear the entire structure, which also marks it as free for the
302 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700303 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700304 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700305 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700306
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700307 return 0;
308}
309
Rusty Russelldde79782007-07-26 10:41:03 -0700310/*L:000
311 * Welcome to our journey through the Launcher!
312 *
313 * The Launcher is the Host userspace program which sets up, runs and services
314 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
315 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000316 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700317 *
318 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
319 * shall see more of that later.
320 *
321 * We begin our understanding with the Host kernel interface which the Launcher
322 * uses: reading and writing a character device called /dev/lguest. All the
323 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700324static struct file_operations lguest_fops = {
325 .owner = THIS_MODULE,
326 .release = close,
327 .write = write,
328 .read = read,
329};
Rusty Russelldde79782007-07-26 10:41:03 -0700330
331/* This is a textbook example of a "misc" character device. Populate a "struct
332 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700333static struct miscdevice lguest_dev = {
334 .minor = MISC_DYNAMIC_MINOR,
335 .name = "lguest",
336 .fops = &lguest_fops,
337};
338
339int __init lguest_device_init(void)
340{
341 return misc_register(&lguest_dev);
342}
343
344void __exit lguest_device_remove(void)
345{
346 misc_deregister(&lguest_dev);
347}