blob: 1982b45bd9357bcf3c6ba8573dba19a1add9707b [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>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -02009#include <linux/sched.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070010#include "lg.h"
11
Rusty Russelle1e72962007-10-25 15:02:50 +100012/*L:055 When something happens, the Waker process needs a way to stop the
13 * kernel running the Guest and return to the Launcher. So the Waker writes
14 * LHREQ_BREAK and the value "1" to /dev/lguest to do this. Once the Launcher
15 * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
16 * the Waker. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020017static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070018{
19 unsigned long on;
20
Rusty Russelle1e72962007-10-25 15:02:50 +100021 /* Fetch whether they're turning break on or off. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070022 if (get_user(on, input) != 0)
23 return -EFAULT;
24
25 if (on) {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020026 cpu->break_out = 1;
Rusty Russella6c372d2009-06-12 22:27:01 -060027 if (!wake_up_process(cpu->tsk))
28 kick_process(cpu->tsk);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070029 /* Wait for them to reset it */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020030 return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070031 } else {
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020032 cpu->break_out = 0;
33 wake_up(&cpu->break_wq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070034 return 0;
35 }
36}
37
Rusty Russelldde79782007-07-26 10:41:03 -070038/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
39 * number to /dev/lguest. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020040static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070041{
Jes Sorensen511801d2007-10-22 11:03:31 +100042 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070043
44 if (get_user(irq, input) != 0)
45 return -EFAULT;
46 if (irq >= LGUEST_IRQS)
47 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -060048
49 set_interrupt(cpu, irq);
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 Russella6bd8e12008-03-28 11:05:53 -050075 /* 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 Russella6bd8e12008-03-28 11:05:53 -050090 /* If we returned from read() last time because the Guest sent I/O,
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
Rusty Russella6bd8e12008-03-28 11:05:53 -050099/*L:025 This actually initializes a CPU. For the moment, a Guest is only
100 * uniprocessor, so "id" is always 0. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200101static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
102{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500103 /* We have a limited number the number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500104 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200105 return -EINVAL;
106
Rusty Russella6bd8e12008-03-28 11:05:53 -0500107 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200108 cpu->id = id;
109 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
110 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500111
112 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200113 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200114
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200115 /* We need a complete page for the Guest registers: they are accessible
116 * to the Guest and we can only grant it access to whole pages. */
117 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
118 if (!cpu->regs_page)
119 return -ENOMEM;
120
121 /* We actually put the registers at the bottom of the page. */
122 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
123
124 /* Now we initialize the Guest's registers, handing it the start
125 * address. */
126 lguest_arch_setup_regs(cpu, start_ip);
127
Rusty Russella6bd8e12008-03-28 11:05:53 -0500128 /* Initialize the queue for the Waker to wait on */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200129 init_waitqueue_head(&cpu->break_wq);
130
131 /* We keep a pointer to the Launcher task (ie. current task) for when
Rusty Russella6bd8e12008-03-28 11:05:53 -0500132 * other Guests want to wake this one (eg. console input). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200133 cpu->tsk = current;
134
135 /* We need to keep a pointer to the Launcher's memory map, because if
136 * the Launcher dies we need to clean it up. If we don't keep a
137 * reference, it is destroyed before close() is called. */
138 cpu->mm = get_task_mm(cpu->tsk);
139
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200140 /* We remember which CPU's pages this Guest used last, for optimization
141 * when the same Guest runs on the same CPU twice. */
142 cpu->last_pages = NULL;
143
Rusty Russella6bd8e12008-03-28 11:05:53 -0500144 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200145 return 0;
146}
147
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300148/*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000149 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700150 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000151 * base: The start of the Guest-physical memory inside the Launcher memory.
152 *
Rusty Russelldde79782007-07-26 10:41:03 -0700153 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000154 * allowed to access. The Guest memory lives inside the Launcher, so it sets
155 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700156 *
Rusty Russelldde79782007-07-26 10:41:03 -0700157 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700158 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000159static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700160{
Rusty Russelldde79782007-07-26 10:41:03 -0700161 /* "struct lguest" contains everything we (the Host) know about a
162 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700163 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000164 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300165 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700166
Rusty Russell48245cc2007-10-22 11:03:27 +1000167 /* We grab the Big Lguest lock, which protects against multiple
168 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700169 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700170 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700171 if (file->private_data) {
172 err = -EBUSY;
173 goto unlock;
174 }
175
176 if (copy_from_user(args, input, sizeof(args)) != 0) {
177 err = -EFAULT;
178 goto unlock;
179 }
180
Rusty Russell48245cc2007-10-22 11:03:27 +1000181 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
182 if (!lg) {
183 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700184 goto unlock;
185 }
Rusty Russelldde79782007-07-26 10:41:03 -0700186
187 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000188 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000189 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700190
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300191 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
192 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200193 if (err)
194 goto release_guest;
195
Rusty Russelldde79782007-07-26 10:41:03 -0700196 /* Initialize the Guest's shadow page tables, using the toplevel
Rusty Russella6bd8e12008-03-28 11:05:53 -0500197 * address the Launcher gave us. This allocates memory, so can fail. */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300198 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700199 if (err)
200 goto free_regs;
201
Rusty Russelldde79782007-07-26 10:41:03 -0700202 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700203 file->private_data = lg;
204
205 mutex_unlock(&lguest_lock);
206
Rusty Russelldde79782007-07-26 10:41:03 -0700207 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700208 return sizeof(args);
209
210free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200211 /* FIXME: This should be in free_vcpu */
212 free_page(lg->cpus[0].regs_page);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700213release_guest:
Adrian Bunk43054412007-11-14 16:59:00 -0800214 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700215unlock:
216 mutex_unlock(&lguest_lock);
217 return err;
218}
219
Rusty Russelldde79782007-07-26 10:41:03 -0700220/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000221 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700222 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella6bd8e12008-03-28 11:05:53 -0500223 * writes of other values to send interrupts.
224 *
225 * Note that we overload the "offset" in the /dev/lguest file to indicate what
226 * CPU number we're dealing with. Currently this is always 0, since we only
227 * support uniprocessor Guests, but you can see the beginnings of SMP support
228 * here. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000229static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700230 size_t size, loff_t *off)
231{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500232 /* Once the Guest is initialized, we hold the "struct lguest" in the
Rusty Russelldde79782007-07-26 10:41:03 -0700233 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700234 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000235 const unsigned long __user *input = (const unsigned long __user *)in;
236 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200237 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200238 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239
Rusty Russella6bd8e12008-03-28 11:05:53 -0500240 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700241 if (get_user(req, input) != 0)
242 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000243 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700244
Rusty Russelldde79782007-07-26 10:41:03 -0700245 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200246 if (req != LHREQ_INITIALIZE) {
247 if (!lg || (cpu_id >= lg->nr_cpus))
248 return -EINVAL;
249 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800250
251 /* Once the Guest is dead, you can only read() why it died. */
252 if (lg->dead)
253 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200254 }
Rusty Russelldde79782007-07-26 10:41:03 -0700255
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700256 switch (req) {
257 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000258 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700259 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200260 return user_send_irq(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700261 case LHREQ_BREAK:
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200262 return break_guest_out(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700263 default:
264 return -EINVAL;
265 }
266}
267
Rusty Russelldde79782007-07-26 10:41:03 -0700268/*L:060 The final piece of interface code is the close() routine. It reverses
269 * everything done in initialize(). This is usually called because the
270 * Launcher exited.
271 *
272 * Note that the close routine returns 0 or a negative error number: it can't
273 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
274 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275static int close(struct inode *inode, struct file *file)
276{
277 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200278 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700279
Rusty Russelldde79782007-07-26 10:41:03 -0700280 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700281 if (!lg)
282 return 0;
283
Rusty Russelldde79782007-07-26 10:41:03 -0700284 /* We need the big lock, to protect from inter-guest I/O and other
285 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700286 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200287
288 /* Free up the shadow page tables for the Guest. */
289 free_guest_pagetable(lg);
290
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200291 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200292 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
293 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200294 /* We can free up the register page we allocated. */
295 free_page(lg->cpus[i].regs_page);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200296 /* Now all the memory cleanups are done, it's safe to release
297 * the Launcher's memory management structure. */
298 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200299 }
Rusty Russelldde79782007-07-26 10:41:03 -0700300 /* If lg->dead doesn't contain an error code it will be NULL or a
301 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700302 if (!IS_ERR(lg->dead))
303 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100304 /* Free the memory allocated to the lguest_struct */
305 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700306 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700307 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700308
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700309 return 0;
310}
311
Rusty Russelldde79782007-07-26 10:41:03 -0700312/*L:000
313 * Welcome to our journey through the Launcher!
314 *
315 * The Launcher is the Host userspace program which sets up, runs and services
316 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
317 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000318 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700319 *
320 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
321 * shall see more of that later.
322 *
323 * We begin our understanding with the Host kernel interface which the Launcher
324 * uses: reading and writing a character device called /dev/lguest. All the
325 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700326static struct file_operations lguest_fops = {
327 .owner = THIS_MODULE,
328 .release = close,
329 .write = write,
330 .read = read,
331};
Rusty Russelldde79782007-07-26 10:41:03 -0700332
333/* This is a textbook example of a "misc" character device. Populate a "struct
334 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700335static struct miscdevice lguest_dev = {
336 .minor = MISC_DYNAMIC_MINOR,
337 .name = "lguest",
338 .fops = &lguest_fops,
339};
340
341int __init lguest_device_init(void)
342{
343 return misc_register(&lguest_dev);
344}
345
346void __exit lguest_device_remove(void)
347{
348 misc_deregister(&lguest_dev);
349}