blob: c4bfe5a2b6b7d320985dab2f9e99de217313ed2b [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. */
Jes Sorensen511801d2007-10-22 11:03:31 +100039static int user_send_irq(struct lguest *lg, 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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070049 set_bit(irq, lg->irqs_pending);
50 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;
58
Rusty Russelldde79782007-07-26 10:41:03 -070059 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070060 if (!lg)
61 return -EINVAL;
62
Rusty Russelle1e72962007-10-25 15:02:50 +100063 /* If you're not the task which owns the Guest, go away. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070064 if (current != lg->tsk)
65 return -EPERM;
66
Rusty Russelldde79782007-07-26 10:41:03 -070067 /* If the guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070068 if (lg->dead) {
69 size_t len;
70
Rusty Russelldde79782007-07-26 10:41:03 -070071 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072 if (IS_ERR(lg->dead))
73 return PTR_ERR(lg->dead);
74
Rusty Russelldde79782007-07-26 10:41:03 -070075 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070076 len = min(size, strlen(lg->dead)+1);
77 if (copy_to_user(user, lg->dead, len) != 0)
78 return -EFAULT;
79 return len;
80 }
81
Rusty Russell15045272007-10-22 11:24:10 +100082 /* If we returned from read() last time because the Guest notified,
Rusty Russelldde79782007-07-26 10:41:03 -070083 * clear the flag. */
Rusty Russell15045272007-10-22 11:24:10 +100084 if (lg->pending_notify)
85 lg->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070086
Rusty Russelldde79782007-07-26 10:41:03 -070087 /* Run the Guest until something interesting happens. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070088 return run_guest(lg, (unsigned long __user *)user);
89}
90
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -020091static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
92{
93 if (id >= NR_CPUS)
94 return -EINVAL;
95
96 cpu->id = id;
97 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
98 cpu->lg->nr_cpus++;
99
100 return 0;
101}
102
Rusty Russell47436aa2007-10-22 11:03:36 +1000103/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000104 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700105 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000106 * base: The start of the Guest-physical memory inside the Launcher memory.
107 *
Rusty Russelldde79782007-07-26 10:41:03 -0700108 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000109 * allowed to access. The Guest memory lives inside the Launcher, so it sets
110 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700111 *
112 * pgdir: The (Guest-physical) address of the top of the initial Guest
113 * pagetables (which are set up by the Launcher).
114 *
115 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700116 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000117static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700118{
Rusty Russelldde79782007-07-26 10:41:03 -0700119 /* "struct lguest" contains everything we (the Host) know about a
120 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700121 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000122 int err;
Rusty Russell47436aa2007-10-22 11:03:36 +1000123 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124
Rusty Russell48245cc2007-10-22 11:03:27 +1000125 /* We grab the Big Lguest lock, which protects against multiple
126 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700127 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700128 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700129 if (file->private_data) {
130 err = -EBUSY;
131 goto unlock;
132 }
133
134 if (copy_from_user(args, input, sizeof(args)) != 0) {
135 err = -EFAULT;
136 goto unlock;
137 }
138
Rusty Russell48245cc2007-10-22 11:03:27 +1000139 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
140 if (!lg) {
141 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700142 goto unlock;
143 }
Rusty Russelldde79782007-07-26 10:41:03 -0700144
145 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000146 lg->mem_base = (void __user *)(long)args[0];
147 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700148
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200149 /* This is the first cpu */
150 err = cpu_start(&lg->cpus[0], 0, args[3]);
151 if (err)
152 goto release_guest;
153
Rusty Russelldde79782007-07-26 10:41:03 -0700154 /* We need a complete page for the Guest registers: they are accessible
155 * to the Guest and we can only grant it access to whole pages. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700156 lg->regs_page = get_zeroed_page(GFP_KERNEL);
157 if (!lg->regs_page) {
158 err = -ENOMEM;
159 goto release_guest;
160 }
Rusty Russelldde79782007-07-26 10:41:03 -0700161 /* We actually put the registers at the bottom of the page. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700162 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
163
Rusty Russelldde79782007-07-26 10:41:03 -0700164 /* Initialize the Guest's shadow page tables, using the toplevel
165 * address the Launcher gave us. This allocates memory, so can
166 * fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000167 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700168 if (err)
169 goto free_regs;
170
Rusty Russelldde79782007-07-26 10:41:03 -0700171 /* Now we initialize the Guest's registers, handing it the start
172 * address. */
Jes Sorensend612cde2007-10-22 11:03:32 +1000173 lguest_arch_setup_regs(lg, args[3]);
Rusty Russelldde79782007-07-26 10:41:03 -0700174
175 /* The timer for lguest's clock needs initialization. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700176 init_clockdev(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700177
178 /* We keep a pointer to the Launcher task (ie. current task) for when
179 * other Guests want to wake this one (inter-Guest I/O). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700180 lg->tsk = current;
Rusty Russelldde79782007-07-26 10:41:03 -0700181 /* We need to keep a pointer to the Launcher's memory map, because if
182 * the Launcher dies we need to clean it up. If we don't keep a
183 * reference, it is destroyed before close() is called. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700184 lg->mm = get_task_mm(lg->tsk);
Rusty Russelldde79782007-07-26 10:41:03 -0700185
186 /* Initialize the queue for the waker to wait on */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700187 init_waitqueue_head(&lg->break_wq);
Rusty Russelldde79782007-07-26 10:41:03 -0700188
189 /* We remember which CPU's pages this Guest used last, for optimization
190 * when the same Guest runs on the same CPU twice. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700191 lg->last_pages = NULL;
Rusty Russelldde79782007-07-26 10:41:03 -0700192
193 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700194 file->private_data = lg;
195
196 mutex_unlock(&lguest_lock);
197
Rusty Russelldde79782007-07-26 10:41:03 -0700198 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700199 return sizeof(args);
200
201free_regs:
202 free_page(lg->regs_page);
203release_guest:
Adrian Bunk43054412007-11-14 16:59:00 -0800204 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700205unlock:
206 mutex_unlock(&lguest_lock);
207 return err;
208}
209
Rusty Russelldde79782007-07-26 10:41:03 -0700210/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000211 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700212 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russell15045272007-10-22 11:24:10 +1000213 * writes of other values to send interrupts. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000214static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700215 size_t size, loff_t *off)
216{
Rusty Russelldde79782007-07-26 10:41:03 -0700217 /* Once the guest is initialized, we hold the "struct lguest" in the
218 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700219 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000220 const unsigned long __user *input = (const unsigned long __user *)in;
221 unsigned long req;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700222
223 if (get_user(req, input) != 0)
224 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000225 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700226
Rusty Russelldde79782007-07-26 10:41:03 -0700227 /* If you haven't initialized, you must do that first. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228 if (req != LHREQ_INITIALIZE && !lg)
229 return -EINVAL;
Rusty Russelldde79782007-07-26 10:41:03 -0700230
231 /* Once the Guest is dead, all you can do is read() why it died. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700232 if (lg && lg->dead)
233 return -ENOENT;
234
235 /* If you're not the task which owns the Guest, you can only break */
236 if (lg && current != lg->tsk && req != LHREQ_BREAK)
237 return -EPERM;
238
239 switch (req) {
240 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000241 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700242 case LHREQ_IRQ:
Jes Sorensen511801d2007-10-22 11:03:31 +1000243 return user_send_irq(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700244 case LHREQ_BREAK:
Jes Sorensen511801d2007-10-22 11:03:31 +1000245 return break_guest_out(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700246 default:
247 return -EINVAL;
248 }
249}
250
Rusty Russelldde79782007-07-26 10:41:03 -0700251/*L:060 The final piece of interface code is the close() routine. It reverses
252 * everything done in initialize(). This is usually called because the
253 * Launcher exited.
254 *
255 * Note that the close routine returns 0 or a negative error number: it can't
256 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
257 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700258static int close(struct inode *inode, struct file *file)
259{
260 struct lguest *lg = file->private_data;
261
Rusty Russelldde79782007-07-26 10:41:03 -0700262 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700263 if (!lg)
264 return 0;
265
Rusty Russelldde79782007-07-26 10:41:03 -0700266 /* We need the big lock, to protect from inter-guest I/O and other
267 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700268 mutex_lock(&lguest_lock);
269 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
270 hrtimer_cancel(&lg->hrt);
Rusty Russelldde79782007-07-26 10:41:03 -0700271 /* Free up the shadow page tables for the Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700272 free_guest_pagetable(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700273 /* Now all the memory cleanups are done, it's safe to release the
274 * Launcher's memory management structure. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275 mmput(lg->mm);
Rusty Russelldde79782007-07-26 10:41:03 -0700276 /* If lg->dead doesn't contain an error code it will be NULL or a
277 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700278 if (!IS_ERR(lg->dead))
279 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700280 /* We can free up the register page we allocated. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700281 free_page(lg->regs_page);
Rusty Russelldde79782007-07-26 10:41:03 -0700282 /* We clear the entire structure, which also marks it as free for the
283 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700284 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700285 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700286 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700287
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700288 return 0;
289}
290
Rusty Russelldde79782007-07-26 10:41:03 -0700291/*L:000
292 * Welcome to our journey through the Launcher!
293 *
294 * The Launcher is the Host userspace program which sets up, runs and services
295 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
296 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000297 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700298 *
299 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
300 * shall see more of that later.
301 *
302 * We begin our understanding with the Host kernel interface which the Launcher
303 * uses: reading and writing a character device called /dev/lguest. All the
304 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700305static struct file_operations lguest_fops = {
306 .owner = THIS_MODULE,
307 .release = close,
308 .write = write,
309 .read = read,
310};
Rusty Russelldde79782007-07-26 10:41:03 -0700311
312/* This is a textbook example of a "misc" character device. Populate a "struct
313 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700314static struct miscdevice lguest_dev = {
315 .minor = MISC_DYNAMIC_MINOR,
316 .name = "lguest",
317 .fops = &lguest_fops,
318};
319
320int __init lguest_device_init(void)
321{
322 return misc_register(&lguest_dev);
323}
324
325void __exit lguest_device_remove(void)
326{
327 misc_deregister(&lguest_dev);
328}