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; |
a6c372de RR |
27 | if (!wake_up_process(cpu->tsk)) |
28 | kick_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; | |
9f155a9b RR |
48 | |
49 | set_interrupt(cpu, irq); | |
d7e28ffe RR |
50 | return 0; |
51 | } | |
52 | ||
dde79789 RR |
53 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading |
54 | * from /dev/lguest. */ | |
d7e28ffe RR |
55 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
56 | { | |
57 | struct lguest *lg = file->private_data; | |
d0953d42 GOC |
58 | struct lg_cpu *cpu; |
59 | unsigned int cpu_id = *o; | |
d7e28ffe | 60 | |
dde79789 | 61 | /* You must write LHREQ_INITIALIZE first! */ |
d7e28ffe RR |
62 | if (!lg) |
63 | return -EINVAL; | |
64 | ||
d0953d42 GOC |
65 | /* Watch out for arbitrary vcpu indexes! */ |
66 | if (cpu_id >= lg->nr_cpus) | |
67 | return -EINVAL; | |
68 | ||
69 | cpu = &lg->cpus[cpu_id]; | |
70 | ||
e1e72965 | 71 | /* If you're not the task which owns the Guest, go away. */ |
66686c2a | 72 | if (current != cpu->tsk) |
d7e28ffe RR |
73 | return -EPERM; |
74 | ||
a6bd8e13 | 75 | /* If the Guest is already dead, we indicate why */ |
d7e28ffe RR |
76 | if (lg->dead) { |
77 | size_t len; | |
78 | ||
dde79789 | 79 | /* lg->dead either contains an error code, or a string. */ |
d7e28ffe RR |
80 | if (IS_ERR(lg->dead)) |
81 | return PTR_ERR(lg->dead); | |
82 | ||
dde79789 | 83 | /* We can only return as much as the buffer they read with. */ |
d7e28ffe RR |
84 | 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 | ||
a6bd8e13 | 90 | /* If we returned from read() last time because the Guest sent I/O, |
dde79789 | 91 | * clear the flag. */ |
5e232f4f GOC |
92 | if (cpu->pending_notify) |
93 | cpu->pending_notify = 0; | |
d7e28ffe | 94 | |
dde79789 | 95 | /* Run the Guest until something interesting happens. */ |
d0953d42 | 96 | return run_guest(cpu, (unsigned long __user *)user); |
d7e28ffe RR |
97 | } |
98 | ||
a6bd8e13 RR |
99 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only |
100 | * uniprocessor, so "id" is always 0. */ | |
4dcc53da GOC |
101 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
102 | { | |
a6bd8e13 | 103 | /* We have a limited number the number of CPUs in the lguest struct. */ |
24adf127 | 104 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
4dcc53da GOC |
105 | return -EINVAL; |
106 | ||
a6bd8e13 | 107 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
4dcc53da GOC |
108 | cpu->id = id; |
109 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); | |
110 | cpu->lg->nr_cpus++; | |
a6bd8e13 RR |
111 | |
112 | /* Each CPU has a timer it can set. */ | |
ad8d8f3b | 113 | init_clockdev(cpu); |
4dcc53da | 114 | |
a53a35a8 GOC |
115 | /* 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 | ||
a6bd8e13 | 128 | /* Initialize the queue for the Waker to wait on */ |
66686c2a GOC |
129 | init_waitqueue_head(&cpu->break_wq); |
130 | ||
131 | /* We keep a pointer to the Launcher task (ie. current task) for when | |
a6bd8e13 | 132 | * other Guests want to wake this one (eg. console input). */ |
66686c2a GOC |
133 | 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 | ||
f34f8c5f GOC |
140 | /* 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 | ||
a6bd8e13 | 144 | /* No error == success. */ |
4dcc53da GOC |
145 | return 0; |
146 | } | |
147 | ||
58a24566 | 148 | /*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) |
511801dc | 149 | * values (in addition to the LHREQ_INITIALIZE value). These are: |
dde79789 | 150 | * |
3c6b5bfa RR |
151 | * base: The start of the Guest-physical memory inside the Launcher memory. |
152 | * | |
dde79789 | 153 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
e1e72965 RR |
154 | * 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. | |
dde79789 | 156 | * |
dde79789 | 157 | * start: The first instruction to execute ("eip" in x86-speak). |
dde79789 | 158 | */ |
511801dc | 159 | static int initialize(struct file *file, const unsigned long __user *input) |
d7e28ffe | 160 | { |
dde79789 RR |
161 | /* "struct lguest" contains everything we (the Host) know about a |
162 | * Guest. */ | |
d7e28ffe | 163 | struct lguest *lg; |
48245cc0 | 164 | int err; |
58a24566 | 165 | unsigned long args[3]; |
d7e28ffe | 166 | |
48245cc0 RR |
167 | /* We grab the Big Lguest lock, which protects against multiple |
168 | * simultaneous initializations. */ | |
d7e28ffe | 169 | mutex_lock(&lguest_lock); |
dde79789 | 170 | /* You can't initialize twice! Close the device and start again... */ |
d7e28ffe RR |
171 | 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 | ||
48245cc0 RR |
181 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
182 | if (!lg) { | |
183 | err = -ENOMEM; | |
d7e28ffe RR |
184 | goto unlock; |
185 | } | |
dde79789 RR |
186 | |
187 | /* Populate the easy fields of our "struct lguest" */ | |
74dbf719 | 188 | lg->mem_base = (void __user *)args[0]; |
3c6b5bfa | 189 | lg->pfn_limit = args[1]; |
dde79789 | 190 | |
58a24566 MZ |
191 | /* 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]); | |
4dcc53da GOC |
193 | if (err) |
194 | goto release_guest; | |
195 | ||
dde79789 | 196 | /* Initialize the Guest's shadow page tables, using the toplevel |
a6bd8e13 | 197 | * address the Launcher gave us. This allocates memory, so can fail. */ |
58a24566 | 198 | err = init_guest_pagetable(lg); |
d7e28ffe RR |
199 | if (err) |
200 | goto free_regs; | |
201 | ||
dde79789 | 202 | /* We keep our "struct lguest" in the file's private_data. */ |
d7e28ffe RR |
203 | file->private_data = lg; |
204 | ||
205 | mutex_unlock(&lguest_lock); | |
206 | ||
dde79789 | 207 | /* And because this is a write() call, we return the length used. */ |
d7e28ffe RR |
208 | return sizeof(args); |
209 | ||
210 | free_regs: | |
a53a35a8 GOC |
211 | /* FIXME: This should be in free_vcpu */ |
212 | free_page(lg->cpus[0].regs_page); | |
d7e28ffe | 213 | release_guest: |
43054412 | 214 | kfree(lg); |
d7e28ffe RR |
215 | unlock: |
216 | mutex_unlock(&lguest_lock); | |
217 | return err; | |
218 | } | |
219 | ||
dde79789 | 220 | /*L:010 The first operation the Launcher does must be a write. All writes |
e1e72965 | 221 | * start with an unsigned long number: for the first write this must be |
dde79789 | 222 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
a6bd8e13 RR |
223 | * 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. */ | |
511801dc | 229 | static ssize_t write(struct file *file, const char __user *in, |
d7e28ffe RR |
230 | size_t size, loff_t *off) |
231 | { | |
a6bd8e13 | 232 | /* Once the Guest is initialized, we hold the "struct lguest" in the |
dde79789 | 233 | * file private data. */ |
d7e28ffe | 234 | struct lguest *lg = file->private_data; |
511801dc JS |
235 | const unsigned long __user *input = (const unsigned long __user *)in; |
236 | unsigned long req; | |
177e449d | 237 | struct lg_cpu *uninitialized_var(cpu); |
7ea07a15 | 238 | unsigned int cpu_id = *off; |
d7e28ffe | 239 | |
a6bd8e13 | 240 | /* The first value tells us what this request is. */ |
d7e28ffe RR |
241 | if (get_user(req, input) != 0) |
242 | return -EFAULT; | |
511801dc | 243 | input++; |
d7e28ffe | 244 | |
dde79789 | 245 | /* If you haven't initialized, you must do that first. */ |
7ea07a15 GOC |
246 | if (req != LHREQ_INITIALIZE) { |
247 | if (!lg || (cpu_id >= lg->nr_cpus)) | |
248 | return -EINVAL; | |
249 | cpu = &lg->cpus[cpu_id]; | |
dde79789 | 250 | |
f73d1e6c ET |
251 | /* Once the Guest is dead, you can only read() why it died. */ |
252 | if (lg->dead) | |
253 | return -ENOENT; | |
f73d1e6c | 254 | } |
d7e28ffe RR |
255 | |
256 | switch (req) { | |
257 | case LHREQ_INITIALIZE: | |
511801dc | 258 | return initialize(file, input); |
d7e28ffe | 259 | case LHREQ_IRQ: |
177e449d | 260 | return user_send_irq(cpu, input); |
d7e28ffe | 261 | case LHREQ_BREAK: |
66686c2a | 262 | return break_guest_out(cpu, input); |
d7e28ffe RR |
263 | default: |
264 | return -EINVAL; | |
265 | } | |
266 | } | |
267 | ||
dde79789 RR |
268 | /*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. :*/ | |
d7e28ffe RR |
275 | static int close(struct inode *inode, struct file *file) |
276 | { | |
277 | struct lguest *lg = file->private_data; | |
ad8d8f3b | 278 | unsigned int i; |
d7e28ffe | 279 | |
dde79789 | 280 | /* If we never successfully initialized, there's nothing to clean up */ |
d7e28ffe RR |
281 | if (!lg) |
282 | return 0; | |
283 | ||
dde79789 RR |
284 | /* We need the big lock, to protect from inter-guest I/O and other |
285 | * Launchers initializing guests. */ | |
d7e28ffe | 286 | mutex_lock(&lguest_lock); |
66686c2a GOC |
287 | |
288 | /* Free up the shadow page tables for the Guest. */ | |
289 | free_guest_pagetable(lg); | |
290 | ||
a53a35a8 | 291 | for (i = 0; i < lg->nr_cpus; i++) { |
ad8d8f3b GOC |
292 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
293 | hrtimer_cancel(&lg->cpus[i].hrt); | |
a53a35a8 GOC |
294 | /* We can free up the register page we allocated. */ |
295 | free_page(lg->cpus[i].regs_page); | |
66686c2a GOC |
296 | /* 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); | |
a53a35a8 | 299 | } |
dde79789 RR |
300 | /* 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(). */ | |
d7e28ffe RR |
302 | if (!IS_ERR(lg->dead)) |
303 | kfree(lg->dead); | |
05dfdbbd MW |
304 | /* Free the memory allocated to the lguest_struct */ |
305 | kfree(lg); | |
dde79789 | 306 | /* Release lock and exit. */ |
d7e28ffe | 307 | mutex_unlock(&lguest_lock); |
dde79789 | 308 | |
d7e28ffe RR |
309 | return 0; |
310 | } | |
311 | ||
dde79789 RR |
312 | /*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 | |
e1e72965 | 318 | * the Guest, but the Guest can't know that. |
dde79789 RR |
319 | * |
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: */ | |
d7e28ffe RR |
326 | static struct file_operations lguest_fops = { |
327 | .owner = THIS_MODULE, | |
328 | .release = close, | |
329 | .write = write, | |
330 | .read = read, | |
331 | }; | |
dde79789 RR |
332 | |
333 | /* This is a textbook example of a "misc" character device. Populate a "struct | |
334 | * miscdevice" and register it with misc_register(). */ | |
d7e28ffe RR |
335 | static struct miscdevice lguest_dev = { |
336 | .minor = MISC_DYNAMIC_MINOR, | |
337 | .name = "lguest", | |
338 | .fops = &lguest_fops, | |
339 | }; | |
340 | ||
341 | int __init lguest_device_init(void) | |
342 | { | |
343 | return misc_register(&lguest_dev); | |
344 | } | |
345 | ||
346 | void __exit lguest_device_remove(void) | |
347 | { | |
348 | misc_deregister(&lguest_dev); | |
349 | } |