Commit | Line | Data |
---|---|---|
f938d2c8 RR |
1 | /*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 | |
3c6b5bfa RR |
3 | * 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 | |
15045275 | 5 | * or the Guest doing a NOTIFY out to the Launcher. :*/ |
d7e28ffe RR |
6 | #include <linux/uaccess.h> |
7 | #include <linux/miscdevice.h> | |
8 | #include <linux/fs.h> | |
ca94f2bd | 9 | #include <linux/sched.h> |
d7e28ffe RR |
10 | #include "lg.h" |
11 | ||
e1e72965 RR |
12 | /*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. */ | |
66686c2a | 17 | static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input) |
d7e28ffe RR |
18 | { |
19 | unsigned long on; | |
20 | ||
e1e72965 | 21 | /* Fetch whether they're turning break on or off. */ |
d7e28ffe RR |
22 | if (get_user(on, input) != 0) |
23 | return -EFAULT; | |
24 | ||
25 | if (on) { | |
66686c2a | 26 | cpu->break_out = 1; |
e1e72965 | 27 | /* Pop it out of the Guest (may be running on different CPU) */ |
66686c2a | 28 | wake_up_process(cpu->tsk); |
d7e28ffe | 29 | /* Wait for them to reset it */ |
66686c2a | 30 | return wait_event_interruptible(cpu->break_wq, !cpu->break_out); |
d7e28ffe | 31 | } else { |
66686c2a GOC |
32 | cpu->break_out = 0; |
33 | wake_up(&cpu->break_wq); | |
d7e28ffe RR |
34 | return 0; |
35 | } | |
36 | } | |
37 | ||
dde79789 RR |
38 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
39 | * number to /dev/lguest. */ | |
177e449d | 40 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
d7e28ffe | 41 | { |
511801dc | 42 | unsigned long irq; |
d7e28ffe RR |
43 | |
44 | if (get_user(irq, input) != 0) | |
45 | return -EFAULT; | |
46 | if (irq >= LGUEST_IRQS) | |
47 | return -EINVAL; | |
dde79789 RR |
48 | /* Next time the Guest runs, the core code will see if it can deliver |
49 | * this interrupt. */ | |
177e449d | 50 | set_bit(irq, cpu->irqs_pending); |
d7e28ffe RR |
51 | return 0; |
52 | } | |
53 | ||
dde79789 RR |
54 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading |
55 | * from /dev/lguest. */ | |
d7e28ffe RR |
56 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
57 | { | |
58 | struct lguest *lg = file->private_data; | |
d0953d42 GOC |
59 | struct lg_cpu *cpu; |
60 | unsigned int cpu_id = *o; | |
d7e28ffe | 61 | |
dde79789 | 62 | /* You must write LHREQ_INITIALIZE first! */ |
d7e28ffe RR |
63 | if (!lg) |
64 | return -EINVAL; | |
65 | ||
d0953d42 GOC |
66 | /* Watch out for arbitrary vcpu indexes! */ |
67 | if (cpu_id >= lg->nr_cpus) | |
68 | return -EINVAL; | |
69 | ||
70 | cpu = &lg->cpus[cpu_id]; | |
71 | ||
e1e72965 | 72 | /* If you're not the task which owns the Guest, go away. */ |
66686c2a | 73 | if (current != cpu->tsk) |
d7e28ffe RR |
74 | return -EPERM; |
75 | ||
a6bd8e13 | 76 | /* If the Guest is already dead, we indicate why */ |
d7e28ffe RR |
77 | if (lg->dead) { |
78 | size_t len; | |
79 | ||
dde79789 | 80 | /* lg->dead either contains an error code, or a string. */ |
d7e28ffe RR |
81 | if (IS_ERR(lg->dead)) |
82 | return PTR_ERR(lg->dead); | |
83 | ||
dde79789 | 84 | /* We can only return as much as the buffer they read with. */ |
d7e28ffe RR |
85 | 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 | ||
a6bd8e13 | 91 | /* If we returned from read() last time because the Guest sent I/O, |
dde79789 | 92 | * clear the flag. */ |
5e232f4f GOC |
93 | if (cpu->pending_notify) |
94 | cpu->pending_notify = 0; | |
d7e28ffe | 95 | |
dde79789 | 96 | /* Run the Guest until something interesting happens. */ |
d0953d42 | 97 | return run_guest(cpu, (unsigned long __user *)user); |
d7e28ffe RR |
98 | } |
99 | ||
a6bd8e13 RR |
100 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only |
101 | * uniprocessor, so "id" is always 0. */ | |
4dcc53da GOC |
102 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
103 | { | |
a6bd8e13 | 104 | /* We have a limited number the number of CPUs in the lguest struct. */ |
24adf127 | 105 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
4dcc53da GOC |
106 | return -EINVAL; |
107 | ||
a6bd8e13 | 108 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
4dcc53da GOC |
109 | cpu->id = id; |
110 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); | |
111 | cpu->lg->nr_cpus++; | |
a6bd8e13 RR |
112 | |
113 | /* Each CPU has a timer it can set. */ | |
ad8d8f3b | 114 | init_clockdev(cpu); |
4dcc53da | 115 | |
a53a35a8 GOC |
116 | /* 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 | ||
a6bd8e13 | 129 | /* Initialize the queue for the Waker to wait on */ |
66686c2a GOC |
130 | init_waitqueue_head(&cpu->break_wq); |
131 | ||
132 | /* We keep a pointer to the Launcher task (ie. current task) for when | |
a6bd8e13 | 133 | * other Guests want to wake this one (eg. console input). */ |
66686c2a GOC |
134 | 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 | ||
f34f8c5f GOC |
141 | /* 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 | ||
a6bd8e13 | 145 | /* No error == success. */ |
4dcc53da GOC |
146 | return 0; |
147 | } | |
148 | ||
47436aa4 | 149 | /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit) |
511801dc | 150 | * values (in addition to the LHREQ_INITIALIZE value). These are: |
dde79789 | 151 | * |
3c6b5bfa RR |
152 | * base: The start of the Guest-physical memory inside the Launcher memory. |
153 | * | |
dde79789 | 154 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
e1e72965 RR |
155 | * 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. | |
dde79789 RR |
157 | * |
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). | |
dde79789 | 162 | */ |
511801dc | 163 | static int initialize(struct file *file, const unsigned long __user *input) |
d7e28ffe | 164 | { |
dde79789 RR |
165 | /* "struct lguest" contains everything we (the Host) know about a |
166 | * Guest. */ | |
d7e28ffe | 167 | struct lguest *lg; |
48245cc0 | 168 | int err; |
47436aa4 | 169 | unsigned long args[4]; |
d7e28ffe | 170 | |
48245cc0 RR |
171 | /* We grab the Big Lguest lock, which protects against multiple |
172 | * simultaneous initializations. */ | |
d7e28ffe | 173 | mutex_lock(&lguest_lock); |
dde79789 | 174 | /* You can't initialize twice! Close the device and start again... */ |
d7e28ffe RR |
175 | 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 | ||
48245cc0 RR |
185 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
186 | if (!lg) { | |
187 | err = -ENOMEM; | |
d7e28ffe RR |
188 | goto unlock; |
189 | } | |
dde79789 RR |
190 | |
191 | /* Populate the easy fields of our "struct lguest" */ | |
74dbf719 | 192 | lg->mem_base = (void __user *)args[0]; |
3c6b5bfa | 193 | lg->pfn_limit = args[1]; |
dde79789 | 194 | |
a6bd8e13 | 195 | /* This is the first cpu (cpu 0) and it will start booting at args[3] */ |
d0953d42 | 196 | err = lg_cpu_start(&lg->cpus[0], 0, args[3]); |
4dcc53da GOC |
197 | if (err) |
198 | goto release_guest; | |
199 | ||
dde79789 | 200 | /* Initialize the Guest's shadow page tables, using the toplevel |
a6bd8e13 | 201 | * address the Launcher gave us. This allocates memory, so can fail. */ |
3c6b5bfa | 202 | err = init_guest_pagetable(lg, args[2]); |
d7e28ffe RR |
203 | if (err) |
204 | goto free_regs; | |
205 | ||
dde79789 | 206 | /* We keep our "struct lguest" in the file's private_data. */ |
d7e28ffe RR |
207 | file->private_data = lg; |
208 | ||
209 | mutex_unlock(&lguest_lock); | |
210 | ||
dde79789 | 211 | /* And because this is a write() call, we return the length used. */ |
d7e28ffe RR |
212 | return sizeof(args); |
213 | ||
214 | free_regs: | |
a53a35a8 GOC |
215 | /* FIXME: This should be in free_vcpu */ |
216 | free_page(lg->cpus[0].regs_page); | |
d7e28ffe | 217 | release_guest: |
43054412 | 218 | kfree(lg); |
d7e28ffe RR |
219 | unlock: |
220 | mutex_unlock(&lguest_lock); | |
221 | return err; | |
222 | } | |
223 | ||
dde79789 | 224 | /*L:010 The first operation the Launcher does must be a write. All writes |
e1e72965 | 225 | * start with an unsigned long number: for the first write this must be |
dde79789 | 226 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
a6bd8e13 RR |
227 | * 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. */ | |
511801dc | 233 | static ssize_t write(struct file *file, const char __user *in, |
d7e28ffe RR |
234 | size_t size, loff_t *off) |
235 | { | |
a6bd8e13 | 236 | /* Once the Guest is initialized, we hold the "struct lguest" in the |
dde79789 | 237 | * file private data. */ |
d7e28ffe | 238 | struct lguest *lg = file->private_data; |
511801dc JS |
239 | const unsigned long __user *input = (const unsigned long __user *)in; |
240 | unsigned long req; | |
177e449d | 241 | struct lg_cpu *uninitialized_var(cpu); |
7ea07a15 | 242 | unsigned int cpu_id = *off; |
d7e28ffe | 243 | |
a6bd8e13 | 244 | /* The first value tells us what this request is. */ |
d7e28ffe RR |
245 | if (get_user(req, input) != 0) |
246 | return -EFAULT; | |
511801dc | 247 | input++; |
d7e28ffe | 248 | |
dde79789 | 249 | /* If you haven't initialized, you must do that first. */ |
7ea07a15 GOC |
250 | if (req != LHREQ_INITIALIZE) { |
251 | if (!lg || (cpu_id >= lg->nr_cpus)) | |
252 | return -EINVAL; | |
253 | cpu = &lg->cpus[cpu_id]; | |
dde79789 | 254 | |
f73d1e6c ET |
255 | /* Once the Guest is dead, you can only read() why it died. */ |
256 | if (lg->dead) | |
257 | return -ENOENT; | |
d7e28ffe | 258 | |
f73d1e6c ET |
259 | /* If you're not the task which owns the Guest, all you can do |
260 | * is break the Launcher out of running the Guest. */ | |
261 | if (current != cpu->tsk && req != LHREQ_BREAK) | |
262 | return -EPERM; | |
263 | } | |
d7e28ffe RR |
264 | |
265 | switch (req) { | |
266 | case LHREQ_INITIALIZE: | |
511801dc | 267 | return initialize(file, input); |
d7e28ffe | 268 | case LHREQ_IRQ: |
177e449d | 269 | return user_send_irq(cpu, input); |
d7e28ffe | 270 | case LHREQ_BREAK: |
66686c2a | 271 | return break_guest_out(cpu, input); |
d7e28ffe RR |
272 | default: |
273 | return -EINVAL; | |
274 | } | |
275 | } | |
276 | ||
dde79789 RR |
277 | /*L:060 The final piece of interface code is the close() routine. It reverses |
278 | * everything done in initialize(). This is usually called because the | |
279 | * Launcher exited. | |
280 | * | |
281 | * Note that the close routine returns 0 or a negative error number: it can't | |
282 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | |
283 | * letting them do it. :*/ | |
d7e28ffe RR |
284 | static int close(struct inode *inode, struct file *file) |
285 | { | |
286 | struct lguest *lg = file->private_data; | |
ad8d8f3b | 287 | unsigned int i; |
d7e28ffe | 288 | |
dde79789 | 289 | /* If we never successfully initialized, there's nothing to clean up */ |
d7e28ffe RR |
290 | if (!lg) |
291 | return 0; | |
292 | ||
dde79789 RR |
293 | /* We need the big lock, to protect from inter-guest I/O and other |
294 | * Launchers initializing guests. */ | |
d7e28ffe | 295 | mutex_lock(&lguest_lock); |
66686c2a GOC |
296 | |
297 | /* Free up the shadow page tables for the Guest. */ | |
298 | free_guest_pagetable(lg); | |
299 | ||
a53a35a8 | 300 | for (i = 0; i < lg->nr_cpus; i++) { |
ad8d8f3b GOC |
301 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
302 | hrtimer_cancel(&lg->cpus[i].hrt); | |
a53a35a8 GOC |
303 | /* We can free up the register page we allocated. */ |
304 | free_page(lg->cpus[i].regs_page); | |
66686c2a GOC |
305 | /* Now all the memory cleanups are done, it's safe to release |
306 | * the Launcher's memory management structure. */ | |
307 | mmput(lg->cpus[i].mm); | |
a53a35a8 | 308 | } |
dde79789 RR |
309 | /* If lg->dead doesn't contain an error code it will be NULL or a |
310 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ | |
d7e28ffe RR |
311 | if (!IS_ERR(lg->dead)) |
312 | kfree(lg->dead); | |
dde79789 RR |
313 | /* We clear the entire structure, which also marks it as free for the |
314 | * next user. */ | |
d7e28ffe | 315 | memset(lg, 0, sizeof(*lg)); |
dde79789 | 316 | /* Release lock and exit. */ |
d7e28ffe | 317 | mutex_unlock(&lguest_lock); |
dde79789 | 318 | |
d7e28ffe RR |
319 | return 0; |
320 | } | |
321 | ||
dde79789 RR |
322 | /*L:000 |
323 | * Welcome to our journey through the Launcher! | |
324 | * | |
325 | * The Launcher is the Host userspace program which sets up, runs and services | |
326 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" | |
327 | * doing things are inaccurate: the Launcher does all the device handling for | |
e1e72965 | 328 | * the Guest, but the Guest can't know that. |
dde79789 RR |
329 | * |
330 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we | |
331 | * shall see more of that later. | |
332 | * | |
333 | * We begin our understanding with the Host kernel interface which the Launcher | |
334 | * uses: reading and writing a character device called /dev/lguest. All the | |
335 | * work happens in the read(), write() and close() routines: */ | |
d7e28ffe RR |
336 | static struct file_operations lguest_fops = { |
337 | .owner = THIS_MODULE, | |
338 | .release = close, | |
339 | .write = write, | |
340 | .read = read, | |
341 | }; | |
dde79789 RR |
342 | |
343 | /* This is a textbook example of a "misc" character device. Populate a "struct | |
344 | * miscdevice" and register it with misc_register(). */ | |
d7e28ffe RR |
345 | static struct miscdevice lguest_dev = { |
346 | .minor = MISC_DYNAMIC_MINOR, | |
347 | .name = "lguest", | |
348 | .fops = &lguest_fops, | |
349 | }; | |
350 | ||
351 | int __init lguest_device_init(void) | |
352 | { | |
353 | return misc_register(&lguest_dev); | |
354 | } | |
355 | ||
356 | void __exit lguest_device_remove(void) | |
357 | { | |
358 | misc_deregister(&lguest_dev); | |
359 | } |