blob: 564e425d71dd37747735e8ba47ed1b1881a022cb [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 Russelle1e72962007-10-25 15:02:50 +100027 /* Pop it out of the Guest (may be running on different CPU) */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020028 wake_up_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 Russelldde79782007-07-26 10:41:03 -070048 /* Next time the Guest runs, the core code will see if it can deliver
49 * this interrupt. */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -020050 set_bit(irq, cpu->irqs_pending);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070051 return 0;
52}
53
Rusty Russelldde79782007-07-26 10:41:03 -070054/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
55 * from /dev/lguest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070056static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
57{
58 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020059 struct lg_cpu *cpu;
60 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070061
Rusty Russelldde79782007-07-26 10:41:03 -070062 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070063 if (!lg)
64 return -EINVAL;
65
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020066 /* Watch out for arbitrary vcpu indexes! */
67 if (cpu_id >= lg->nr_cpus)
68 return -EINVAL;
69
70 cpu = &lg->cpus[cpu_id];
71
Rusty Russelle1e72962007-10-25 15:02:50 +100072 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -020073 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070074 return -EPERM;
75
Rusty Russella6bd8e12008-03-28 11:05:53 -050076 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070077 if (lg->dead) {
78 size_t len;
79
Rusty Russelldde79782007-07-26 10:41:03 -070080 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070081 if (IS_ERR(lg->dead))
82 return PTR_ERR(lg->dead);
83
Rusty Russelldde79782007-07-26 10:41:03 -070084 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070085 len = min(size, strlen(lg->dead)+1);
86 if (copy_to_user(user, lg->dead, len) != 0)
87 return -EFAULT;
88 return len;
89 }
90
Rusty Russella6bd8e12008-03-28 11:05:53 -050091 /* If we returned from read() last time because the Guest sent I/O,
Rusty Russelldde79782007-07-26 10:41:03 -070092 * clear the flag. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -020093 if (cpu->pending_notify)
94 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070095
Rusty Russelldde79782007-07-26 10:41:03 -070096 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -020097 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070098}
99
Rusty Russella6bd8e12008-03-28 11:05:53 -0500100/*L:025 This actually initializes a CPU. For the moment, a Guest is only
101 * uniprocessor, so "id" is always 0. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200102static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
103{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500104 /* We have a limited number the number of CPUs in the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200105 if (id >= NR_CPUS)
106 return -EINVAL;
107
Rusty Russella6bd8e12008-03-28 11:05:53 -0500108 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200109 cpu->id = id;
110 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
111 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500112
113 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200114 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200115
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200116 /* We need a complete page for the Guest registers: they are accessible
117 * to the Guest and we can only grant it access to whole pages. */
118 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
119 if (!cpu->regs_page)
120 return -ENOMEM;
121
122 /* We actually put the registers at the bottom of the page. */
123 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
124
125 /* Now we initialize the Guest's registers, handing it the start
126 * address. */
127 lguest_arch_setup_regs(cpu, start_ip);
128
Rusty Russella6bd8e12008-03-28 11:05:53 -0500129 /* Initialize the queue for the Waker to wait on */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200130 init_waitqueue_head(&cpu->break_wq);
131
132 /* We keep a pointer to the Launcher task (ie. current task) for when
Rusty Russella6bd8e12008-03-28 11:05:53 -0500133 * other Guests want to wake this one (eg. console input). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200134 cpu->tsk = current;
135
136 /* We need to keep a pointer to the Launcher's memory map, because if
137 * the Launcher dies we need to clean it up. If we don't keep a
138 * reference, it is destroyed before close() is called. */
139 cpu->mm = get_task_mm(cpu->tsk);
140
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200141 /* We remember which CPU's pages this Guest used last, for optimization
142 * when the same Guest runs on the same CPU twice. */
143 cpu->last_pages = NULL;
144
Rusty Russella6bd8e12008-03-28 11:05:53 -0500145 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200146 return 0;
147}
148
Rusty Russell47436aa2007-10-22 11:03:36 +1000149/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
Jes Sorensen511801d2007-10-22 11:03:31 +1000150 * values (in addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700151 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000152 * base: The start of the Guest-physical memory inside the Launcher memory.
153 *
Rusty Russelldde79782007-07-26 10:41:03 -0700154 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000155 * allowed to access. The Guest memory lives inside the Launcher, so it sets
156 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700157 *
158 * pgdir: The (Guest-physical) address of the top of the initial Guest
159 * pagetables (which are set up by the Launcher).
160 *
161 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700162 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000163static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700164{
Rusty Russelldde79782007-07-26 10:41:03 -0700165 /* "struct lguest" contains everything we (the Host) know about a
166 * Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700167 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000168 int err;
Rusty Russell47436aa2007-10-22 11:03:36 +1000169 unsigned long args[4];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700170
Rusty Russell48245cc2007-10-22 11:03:27 +1000171 /* We grab the Big Lguest lock, which protects against multiple
172 * simultaneous initializations. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700173 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700174 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 if (file->private_data) {
176 err = -EBUSY;
177 goto unlock;
178 }
179
180 if (copy_from_user(args, input, sizeof(args)) != 0) {
181 err = -EFAULT;
182 goto unlock;
183 }
184
Rusty Russell48245cc2007-10-22 11:03:27 +1000185 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
186 if (!lg) {
187 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700188 goto unlock;
189 }
Rusty Russelldde79782007-07-26 10:41:03 -0700190
191 /* Populate the easy fields of our "struct lguest" */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000192 lg->mem_base = (void __user *)(long)args[0];
193 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700194
Rusty Russella6bd8e12008-03-28 11:05:53 -0500195 /* This is the first cpu (cpu 0) and it will start booting at args[3] */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200196 err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200197 if (err)
198 goto release_guest;
199
Rusty Russelldde79782007-07-26 10:41:03 -0700200 /* Initialize the Guest's shadow page tables, using the toplevel
Rusty Russella6bd8e12008-03-28 11:05:53 -0500201 * address the Launcher gave us. This allocates memory, so can fail. */
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000202 err = init_guest_pagetable(lg, args[2]);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700203 if (err)
204 goto free_regs;
205
Rusty Russelldde79782007-07-26 10:41:03 -0700206 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700207 file->private_data = lg;
208
209 mutex_unlock(&lguest_lock);
210
Rusty Russelldde79782007-07-26 10:41:03 -0700211 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700212 return sizeof(args);
213
214free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200215 /* FIXME: This should be in free_vcpu */
216 free_page(lg->cpus[0].regs_page);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700217release_guest:
Adrian Bunk43054412007-11-14 16:59:00 -0800218 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700219unlock:
220 mutex_unlock(&lguest_lock);
221 return err;
222}
223
Rusty Russelldde79782007-07-26 10:41:03 -0700224/*L:010 The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000225 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700226 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella6bd8e12008-03-28 11:05:53 -0500227 * writes of other values to send interrupts.
228 *
229 * Note that we overload the "offset" in the /dev/lguest file to indicate what
230 * CPU number we're dealing with. Currently this is always 0, since we only
231 * support uniprocessor Guests, but you can see the beginnings of SMP support
232 * here. */
Jes Sorensen511801d2007-10-22 11:03:31 +1000233static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700234 size_t size, loff_t *off)
235{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500236 /* Once the Guest is initialized, we hold the "struct lguest" in the
Rusty Russelldde79782007-07-26 10:41:03 -0700237 * file private data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700238 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000239 const unsigned long __user *input = (const unsigned long __user *)in;
240 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200241 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200242 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700243
Rusty Russella6bd8e12008-03-28 11:05:53 -0500244 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700245 if (get_user(req, input) != 0)
246 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000247 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700248
Rusty Russelldde79782007-07-26 10:41:03 -0700249 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200250 if (req != LHREQ_INITIALIZE) {
251 if (!lg || (cpu_id >= lg->nr_cpus))
252 return -EINVAL;
253 cpu = &lg->cpus[cpu_id];
254 if (!cpu)
255 return -EINVAL;
Eugene Teof73d1e62008-02-09 23:53:17 +0800256
257 /* Once the Guest is dead, you can only read() why it died. */
258 if (lg->dead)
259 return -ENOENT;
260
261 /* If you're not the task which owns the Guest, all you can do
262 * is break the Launcher out of running the Guest. */
263 if (current != cpu->tsk && req != LHREQ_BREAK)
264 return -EPERM;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200265 }
Rusty Russelldde79782007-07-26 10:41:03 -0700266
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700267 switch (req) {
268 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000269 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700270 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200271 return user_send_irq(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700272 case LHREQ_BREAK:
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200273 return break_guest_out(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700274 default:
275 return -EINVAL;
276 }
277}
278
Rusty Russelldde79782007-07-26 10:41:03 -0700279/*L:060 The final piece of interface code is the close() routine. It reverses
280 * everything done in initialize(). This is usually called because the
281 * Launcher exited.
282 *
283 * Note that the close routine returns 0 or a negative error number: it can't
284 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
285 * letting them do it. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700286static int close(struct inode *inode, struct file *file)
287{
288 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200289 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700290
Rusty Russelldde79782007-07-26 10:41:03 -0700291 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 if (!lg)
293 return 0;
294
Rusty Russelldde79782007-07-26 10:41:03 -0700295 /* We need the big lock, to protect from inter-guest I/O and other
296 * Launchers initializing guests. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700297 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200298
299 /* Free up the shadow page tables for the Guest. */
300 free_guest_pagetable(lg);
301
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200302 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200303 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
304 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200305 /* We can free up the register page we allocated. */
306 free_page(lg->cpus[i].regs_page);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200307 /* Now all the memory cleanups are done, it's safe to release
308 * the Launcher's memory management structure. */
309 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200310 }
Rusty Russelldde79782007-07-26 10:41:03 -0700311 /* If lg->dead doesn't contain an error code it will be NULL or a
312 * kmalloc()ed string, either of which is ok to hand to kfree(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700313 if (!IS_ERR(lg->dead))
314 kfree(lg->dead);
Rusty Russelldde79782007-07-26 10:41:03 -0700315 /* We clear the entire structure, which also marks it as free for the
316 * next user. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700317 memset(lg, 0, sizeof(*lg));
Rusty Russelldde79782007-07-26 10:41:03 -0700318 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700319 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700320
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700321 return 0;
322}
323
Rusty Russelldde79782007-07-26 10:41:03 -0700324/*L:000
325 * Welcome to our journey through the Launcher!
326 *
327 * The Launcher is the Host userspace program which sets up, runs and services
328 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
329 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000330 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700331 *
332 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
333 * shall see more of that later.
334 *
335 * We begin our understanding with the Host kernel interface which the Launcher
336 * uses: reading and writing a character device called /dev/lguest. All the
337 * work happens in the read(), write() and close() routines: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700338static struct file_operations lguest_fops = {
339 .owner = THIS_MODULE,
340 .release = close,
341 .write = write,
342 .read = read,
343};
Rusty Russelldde79782007-07-26 10:41:03 -0700344
345/* This is a textbook example of a "misc" character device. Populate a "struct
346 * miscdevice" and register it with misc_register(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700347static struct miscdevice lguest_dev = {
348 .minor = MISC_DYNAMIC_MINOR,
349 .name = "lguest",
350 .fops = &lguest_fops,
351};
352
353int __init lguest_device_init(void)
354{
355 return misc_register(&lguest_dev);
356}
357
358void __exit lguest_device_remove(void)
359{
360 misc_deregister(&lguest_dev);
361}