blob: 605db5c49e7fdb7ac2154718b7ded80d6cfcd956 [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. */
Jes Sorensen511801d2007-10-22 11:03:31 +100016static int break_guest_out(struct lguest *lg, 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) {
25 lg->break_out = 1;
Rusty Russelle1e72962007-10-25 15:02:50 +100026 /* Pop it out of the Guest (may be running on different CPU) */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070027 wake_up_process(lg->tsk);
28 /* Wait for them to reset it */
29 return wait_event_interruptible(lg->break_wq, !lg->break_out);
30 } else {
31 lg->break_out = 0;
32 wake_up(&lg->break_wq);
33 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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072 if (current != lg->tsk)
73 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. */
Rusty Russell15045272007-10-22 11:24:10 +100092 if (lg->pending_notify)
93 lg->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
109 return 0;
110}
111
Rusty Russell47436aa2007-10-22 11:03:36 +1000112/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000113 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700114 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000115 * base: The start of the Guest-physical memory inside the Launcher memory.
116 *
Rusty Russelldde79782007-07-26 10:41:03 -0700117 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000118 * allowed to access. The Guest memory lives inside the Launcher, so it sets
119 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700120 *
121 * pgdir: The (Guest-physical) address of the top of the initial Guest
122 * pagetables (which are set up by the Launcher).
123 *
124 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700125 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000126static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700127{
Rusty Russelldde79782007-07-26 10:41:03 -0700128 /* "struct lguest" contains everything we (the Host) know about a
129 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700130 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000131 int err;
Rusty Russell47436aa2007-10-22 11:03:36 +1000132 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700133
Rusty Russell48245cc2007-10-22 11:03:27 +1000134 /* We grab the Big Lguest lock, which protects against multiple
135 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700136 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700137 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700138 if (file->private_data) {
139 err = -EBUSY;
140 goto unlock;
141 }
142
143 if (copy_from_user(args, input, sizeof(args)) != 0) {
144 err = -EFAULT;
145 goto unlock;
146 }
147
Rusty Russell48245cc2007-10-22 11:03:27 +1000148 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
149 if (!lg) {
150 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700151 goto unlock;
152 }
Rusty Russelldde79782007-07-26 10:41:03 -0700153
154 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000155 lg->mem_base = (void __user *)(long)args[0];
156 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700157
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200158 /* This is the first cpu */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200159 err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200160 if (err)
161 goto release_guest;
162
Rusty Russelldde79782007-07-26 10:41:03 -0700163 /* We need a complete page for the Guest registers: they are accessible
164 * to the Guest and we can only grant it access to whole pages. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700165 lg->regs_page = get_zeroed_page(GFP_KERNEL);
166 if (!lg->regs_page) {
167 err = -ENOMEM;
168 goto release_guest;
169 }
Rusty Russelldde79782007-07-26 10:41:03 -0700170 /* We actually put the registers at the bottom of the page. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700171 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
172
Rusty Russelldde79782007-07-26 10:41:03 -0700173 /* Initialize the Guest's shadow page tables, using the toplevel
174 * address the Launcher gave us. This allocates memory, so can
175 * fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000176 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700177 if (err)
178 goto free_regs;
179
Rusty Russelldde79782007-07-26 10:41:03 -0700180 /* Now we initialize the Guest's registers, handing it the start
181 * address. */
Jes Sorensend612cde2007-10-22 11:03:32 +1000182 lguest_arch_setup_regs(lg, args[3]);
Rusty Russelldde79782007-07-26 10:41:03 -0700183
Rusty Russelldde79782007-07-26 10:41:03 -0700184 /* We keep a pointer to the Launcher task (ie. current task) for when
185 * other Guests want to wake this one (inter-Guest I/O). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700186 lg->tsk = current;
Rusty Russelldde79782007-07-26 10:41:03 -0700187 /* We need to keep a pointer to the Launcher's memory map, because if
188 * the Launcher dies we need to clean it up. If we don't keep a
189 * reference, it is destroyed before close() is called. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700190 lg->mm = get_task_mm(lg->tsk);
Rusty Russelldde79782007-07-26 10:41:03 -0700191
192 /* Initialize the queue for the waker to wait on */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700193 init_waitqueue_head(&lg->break_wq);
Rusty Russelldde79782007-07-26 10:41:03 -0700194
195 /* We remember which CPU's pages this Guest used last, for optimization
196 * when the same Guest runs on the same CPU twice. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700197 lg->last_pages = NULL;
Rusty Russelldde79782007-07-26 10:41:03 -0700198
199 /* 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:
208 free_page(lg->regs_page);
209release_guest:
Adrian Bunk43054412007-11-14 16:59:00 -0800210 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700211unlock:
212 mutex_unlock(&lguest_lock);
213 return err;
214}
215
Rusty Russelldde79782007-07-26 10:41:03 -0700216/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000217 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700218 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russell15045272007-10-22 11:24:10 +1000219 * writes of other values to send interrupts. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000220static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700221 size_t size, loff_t *off)
222{
Rusty Russelldde79782007-07-26 10:41:03 -0700223 /* Once the guest is initialized, we hold the "struct lguest" in the
224 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700225 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000226 const unsigned long __user *input = (const unsigned long __user *)in;
227 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200228 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200229 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700230
231 if (get_user(req, input) != 0)
232 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000233 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700234
Rusty Russelldde79782007-07-26 10:41:03 -0700235 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200236 if (req != LHREQ_INITIALIZE) {
237 if (!lg || (cpu_id >= lg->nr_cpus))
238 return -EINVAL;
239 cpu = &lg->cpus[cpu_id];
240 if (!cpu)
241 return -EINVAL;
242 }
Rusty Russelldde79782007-07-26 10:41:03 -0700243
244 /* Once the Guest is dead, all you can do is read() why it died. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700245 if (lg && lg->dead)
246 return -ENOENT;
247
248 /* If you're not the task which owns the Guest, you can only break */
249 if (lg && current != lg->tsk && req != LHREQ_BREAK)
250 return -EPERM;
251
252 switch (req) {
253 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000254 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700255 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200256 return user_send_irq(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700257 case LHREQ_BREAK:
Jes Sorensen511801d2007-10-22 11:03:31 +1000258 return break_guest_out(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700259 default:
260 return -EINVAL;
261 }
262}
263
Rusty Russelldde79782007-07-26 10:41:03 -0700264/*L:060 The final piece of interface code is the close() routine. It reverses
265 * everything done in initialize(). This is usually called because the
266 * Launcher exited.
267 *
268 * Note that the close routine returns 0 or a negative error number: it can't
269 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
270 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700271static int close(struct inode *inode, struct file *file)
272{
273 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200274 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275
Rusty Russelldde79782007-07-26 10:41:03 -0700276 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700277 if (!lg)
278 return 0;
279
Rusty Russelldde79782007-07-26 10:41:03 -0700280 /* We need the big lock, to protect from inter-guest I/O and other
281 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700282 mutex_lock(&lguest_lock);
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200283 for (i = 0; i < lg->nr_cpus; i++)
284 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
285 hrtimer_cancel(&lg->cpus[i].hrt);
Rusty Russelldde79782007-07-26 10:41:03 -0700286 /* Free up the shadow page tables for the Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700287 free_guest_pagetable(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700288 /* Now all the memory cleanups are done, it's safe to release the
289 * Launcher's memory management structure. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700290 mmput(lg->mm);
Rusty Russelldde79782007-07-26 10:41:03 -0700291 /* If lg->dead doesn't contain an error code it will be NULL or a
292 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700293 if (!IS_ERR(lg->dead))
294 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700295 /* We can free up the register page we allocated. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700296 free_page(lg->regs_page);
Rusty Russelldde79782007-07-26 10:41:03 -0700297 /* We clear the entire structure, which also marks it as free for the
298 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700299 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700300 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700301 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700302
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700303 return 0;
304}
305
Rusty Russelldde79782007-07-26 10:41:03 -0700306/*L:000
307 * Welcome to our journey through the Launcher!
308 *
309 * The Launcher is the Host userspace program which sets up, runs and services
310 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
311 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000312 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700313 *
314 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
315 * shall see more of that later.
316 *
317 * We begin our understanding with the Host kernel interface which the Launcher
318 * uses: reading and writing a character device called /dev/lguest. All the
319 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700320static struct file_operations lguest_fops = {
321 .owner = THIS_MODULE,
322 .release = close,
323 .write = write,
324 .read = read,
325};
Rusty Russelldde79782007-07-26 10:41:03 -0700326
327/* This is a textbook example of a "misc" character device. Populate a "struct
328 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700329static struct miscdevice lguest_dev = {
330 .minor = MISC_DYNAMIC_MINOR,
331 .name = "lguest",
332 .fops = &lguest_fops,
333};
334
335int __init lguest_device_init(void)
336{
337 return misc_register(&lguest_dev);
338}
339
340void __exit lguest_device_remove(void)
341{
342 misc_deregister(&lguest_dev);
343}