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> | |
9 | #include "lg.h" | |
10 | ||
e1e72965 RR |
11 | /*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. */ | |
511801dc | 16 | static int break_guest_out(struct lguest *lg, const unsigned long __user *input) |
d7e28ffe RR |
17 | { |
18 | unsigned long on; | |
19 | ||
e1e72965 | 20 | /* Fetch whether they're turning break on or off. */ |
d7e28ffe RR |
21 | if (get_user(on, input) != 0) |
22 | return -EFAULT; | |
23 | ||
24 | if (on) { | |
25 | lg->break_out = 1; | |
e1e72965 | 26 | /* Pop it out of the Guest (may be running on different CPU) */ |
d7e28ffe RR |
27 | 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 | ||
dde79789 RR |
37 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
38 | * number to /dev/lguest. */ | |
511801dc | 39 | static int user_send_irq(struct lguest *lg, const unsigned long __user *input) |
d7e28ffe | 40 | { |
511801dc | 41 | unsigned long irq; |
d7e28ffe RR |
42 | |
43 | if (get_user(irq, input) != 0) | |
44 | return -EFAULT; | |
45 | if (irq >= LGUEST_IRQS) | |
46 | return -EINVAL; | |
dde79789 RR |
47 | /* Next time the Guest runs, the core code will see if it can deliver |
48 | * this interrupt. */ | |
d7e28ffe RR |
49 | set_bit(irq, lg->irqs_pending); |
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; | |
58 | ||
dde79789 | 59 | /* You must write LHREQ_INITIALIZE first! */ |
d7e28ffe RR |
60 | if (!lg) |
61 | return -EINVAL; | |
62 | ||
e1e72965 | 63 | /* If you're not the task which owns the Guest, go away. */ |
d7e28ffe RR |
64 | if (current != lg->tsk) |
65 | return -EPERM; | |
66 | ||
dde79789 | 67 | /* If the guest is already dead, we indicate why */ |
d7e28ffe RR |
68 | if (lg->dead) { |
69 | size_t len; | |
70 | ||
dde79789 | 71 | /* lg->dead either contains an error code, or a string. */ |
d7e28ffe RR |
72 | if (IS_ERR(lg->dead)) |
73 | return PTR_ERR(lg->dead); | |
74 | ||
dde79789 | 75 | /* We can only return as much as the buffer they read with. */ |
d7e28ffe RR |
76 | 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 | ||
15045275 | 82 | /* If we returned from read() last time because the Guest notified, |
dde79789 | 83 | * clear the flag. */ |
15045275 RR |
84 | if (lg->pending_notify) |
85 | lg->pending_notify = 0; | |
d7e28ffe | 86 | |
dde79789 | 87 | /* Run the Guest until something interesting happens. */ |
d7e28ffe RR |
88 | return run_guest(lg, (unsigned long __user *)user); |
89 | } | |
90 | ||
47436aa4 | 91 | /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit) |
511801dc | 92 | * values (in addition to the LHREQ_INITIALIZE value). These are: |
dde79789 | 93 | * |
3c6b5bfa RR |
94 | * base: The start of the Guest-physical memory inside the Launcher memory. |
95 | * | |
dde79789 | 96 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
e1e72965 RR |
97 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
98 | * this to ensure the Guest can only reach its own memory. | |
dde79789 RR |
99 | * |
100 | * pgdir: The (Guest-physical) address of the top of the initial Guest | |
101 | * pagetables (which are set up by the Launcher). | |
102 | * | |
103 | * start: The first instruction to execute ("eip" in x86-speak). | |
dde79789 | 104 | */ |
511801dc | 105 | static int initialize(struct file *file, const unsigned long __user *input) |
d7e28ffe | 106 | { |
dde79789 RR |
107 | /* "struct lguest" contains everything we (the Host) know about a |
108 | * Guest. */ | |
d7e28ffe | 109 | struct lguest *lg; |
48245cc0 | 110 | int err; |
47436aa4 | 111 | unsigned long args[4]; |
d7e28ffe | 112 | |
48245cc0 RR |
113 | /* We grab the Big Lguest lock, which protects against multiple |
114 | * simultaneous initializations. */ | |
d7e28ffe | 115 | mutex_lock(&lguest_lock); |
dde79789 | 116 | /* You can't initialize twice! Close the device and start again... */ |
d7e28ffe RR |
117 | if (file->private_data) { |
118 | err = -EBUSY; | |
119 | goto unlock; | |
120 | } | |
121 | ||
122 | if (copy_from_user(args, input, sizeof(args)) != 0) { | |
123 | err = -EFAULT; | |
124 | goto unlock; | |
125 | } | |
126 | ||
48245cc0 RR |
127 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
128 | if (!lg) { | |
129 | err = -ENOMEM; | |
d7e28ffe RR |
130 | goto unlock; |
131 | } | |
dde79789 RR |
132 | |
133 | /* Populate the easy fields of our "struct lguest" */ | |
3c6b5bfa RR |
134 | lg->mem_base = (void __user *)(long)args[0]; |
135 | lg->pfn_limit = args[1]; | |
dde79789 RR |
136 | |
137 | /* We need a complete page for the Guest registers: they are accessible | |
138 | * to the Guest and we can only grant it access to whole pages. */ | |
d7e28ffe RR |
139 | lg->regs_page = get_zeroed_page(GFP_KERNEL); |
140 | if (!lg->regs_page) { | |
141 | err = -ENOMEM; | |
142 | goto release_guest; | |
143 | } | |
dde79789 | 144 | /* We actually put the registers at the bottom of the page. */ |
d7e28ffe RR |
145 | lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs); |
146 | ||
dde79789 RR |
147 | /* Initialize the Guest's shadow page tables, using the toplevel |
148 | * address the Launcher gave us. This allocates memory, so can | |
149 | * fail. */ | |
3c6b5bfa | 150 | err = init_guest_pagetable(lg, args[2]); |
d7e28ffe RR |
151 | if (err) |
152 | goto free_regs; | |
153 | ||
dde79789 RR |
154 | /* Now we initialize the Guest's registers, handing it the start |
155 | * address. */ | |
d612cde0 | 156 | lguest_arch_setup_regs(lg, args[3]); |
dde79789 RR |
157 | |
158 | /* The timer for lguest's clock needs initialization. */ | |
d7e28ffe | 159 | init_clockdev(lg); |
dde79789 RR |
160 | |
161 | /* We keep a pointer to the Launcher task (ie. current task) for when | |
162 | * other Guests want to wake this one (inter-Guest I/O). */ | |
d7e28ffe | 163 | lg->tsk = current; |
dde79789 RR |
164 | /* We need to keep a pointer to the Launcher's memory map, because if |
165 | * the Launcher dies we need to clean it up. If we don't keep a | |
166 | * reference, it is destroyed before close() is called. */ | |
d7e28ffe | 167 | lg->mm = get_task_mm(lg->tsk); |
dde79789 RR |
168 | |
169 | /* Initialize the queue for the waker to wait on */ | |
d7e28ffe | 170 | init_waitqueue_head(&lg->break_wq); |
dde79789 RR |
171 | |
172 | /* We remember which CPU's pages this Guest used last, for optimization | |
173 | * when the same Guest runs on the same CPU twice. */ | |
d7e28ffe | 174 | lg->last_pages = NULL; |
dde79789 RR |
175 | |
176 | /* We keep our "struct lguest" in the file's private_data. */ | |
d7e28ffe RR |
177 | file->private_data = lg; |
178 | ||
179 | mutex_unlock(&lguest_lock); | |
180 | ||
dde79789 | 181 | /* And because this is a write() call, we return the length used. */ |
d7e28ffe RR |
182 | return sizeof(args); |
183 | ||
184 | free_regs: | |
185 | free_page(lg->regs_page); | |
186 | release_guest: | |
43054412 | 187 | kfree(lg); |
d7e28ffe RR |
188 | unlock: |
189 | mutex_unlock(&lguest_lock); | |
190 | return err; | |
191 | } | |
192 | ||
dde79789 | 193 | /*L:010 The first operation the Launcher does must be a write. All writes |
e1e72965 | 194 | * start with an unsigned long number: for the first write this must be |
dde79789 | 195 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
15045275 | 196 | * writes of other values to send interrupts. */ |
511801dc | 197 | static ssize_t write(struct file *file, const char __user *in, |
d7e28ffe RR |
198 | size_t size, loff_t *off) |
199 | { | |
dde79789 RR |
200 | /* Once the guest is initialized, we hold the "struct lguest" in the |
201 | * file private data. */ | |
d7e28ffe | 202 | struct lguest *lg = file->private_data; |
511801dc JS |
203 | const unsigned long __user *input = (const unsigned long __user *)in; |
204 | unsigned long req; | |
d7e28ffe RR |
205 | |
206 | if (get_user(req, input) != 0) | |
207 | return -EFAULT; | |
511801dc | 208 | input++; |
d7e28ffe | 209 | |
dde79789 | 210 | /* If you haven't initialized, you must do that first. */ |
d7e28ffe RR |
211 | if (req != LHREQ_INITIALIZE && !lg) |
212 | return -EINVAL; | |
dde79789 RR |
213 | |
214 | /* Once the Guest is dead, all you can do is read() why it died. */ | |
d7e28ffe RR |
215 | if (lg && lg->dead) |
216 | return -ENOENT; | |
217 | ||
218 | /* If you're not the task which owns the Guest, you can only break */ | |
219 | if (lg && current != lg->tsk && req != LHREQ_BREAK) | |
220 | return -EPERM; | |
221 | ||
222 | switch (req) { | |
223 | case LHREQ_INITIALIZE: | |
511801dc | 224 | return initialize(file, input); |
d7e28ffe | 225 | case LHREQ_IRQ: |
511801dc | 226 | return user_send_irq(lg, input); |
d7e28ffe | 227 | case LHREQ_BREAK: |
511801dc | 228 | return break_guest_out(lg, input); |
d7e28ffe RR |
229 | default: |
230 | return -EINVAL; | |
231 | } | |
232 | } | |
233 | ||
dde79789 RR |
234 | /*L:060 The final piece of interface code is the close() routine. It reverses |
235 | * everything done in initialize(). This is usually called because the | |
236 | * Launcher exited. | |
237 | * | |
238 | * Note that the close routine returns 0 or a negative error number: it can't | |
239 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | |
240 | * letting them do it. :*/ | |
d7e28ffe RR |
241 | static int close(struct inode *inode, struct file *file) |
242 | { | |
243 | struct lguest *lg = file->private_data; | |
244 | ||
dde79789 | 245 | /* If we never successfully initialized, there's nothing to clean up */ |
d7e28ffe RR |
246 | if (!lg) |
247 | return 0; | |
248 | ||
dde79789 RR |
249 | /* We need the big lock, to protect from inter-guest I/O and other |
250 | * Launchers initializing guests. */ | |
d7e28ffe RR |
251 | mutex_lock(&lguest_lock); |
252 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ | |
253 | hrtimer_cancel(&lg->hrt); | |
dde79789 | 254 | /* Free up the shadow page tables for the Guest. */ |
d7e28ffe | 255 | free_guest_pagetable(lg); |
dde79789 RR |
256 | /* Now all the memory cleanups are done, it's safe to release the |
257 | * Launcher's memory management structure. */ | |
d7e28ffe | 258 | mmput(lg->mm); |
dde79789 RR |
259 | /* If lg->dead doesn't contain an error code it will be NULL or a |
260 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ | |
d7e28ffe RR |
261 | if (!IS_ERR(lg->dead)) |
262 | kfree(lg->dead); | |
dde79789 | 263 | /* We can free up the register page we allocated. */ |
d7e28ffe | 264 | free_page(lg->regs_page); |
dde79789 RR |
265 | /* We clear the entire structure, which also marks it as free for the |
266 | * next user. */ | |
d7e28ffe | 267 | memset(lg, 0, sizeof(*lg)); |
dde79789 | 268 | /* Release lock and exit. */ |
d7e28ffe | 269 | mutex_unlock(&lguest_lock); |
dde79789 | 270 | |
d7e28ffe RR |
271 | return 0; |
272 | } | |
273 | ||
dde79789 RR |
274 | /*L:000 |
275 | * Welcome to our journey through the Launcher! | |
276 | * | |
277 | * The Launcher is the Host userspace program which sets up, runs and services | |
278 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" | |
279 | * doing things are inaccurate: the Launcher does all the device handling for | |
e1e72965 | 280 | * the Guest, but the Guest can't know that. |
dde79789 RR |
281 | * |
282 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we | |
283 | * shall see more of that later. | |
284 | * | |
285 | * We begin our understanding with the Host kernel interface which the Launcher | |
286 | * uses: reading and writing a character device called /dev/lguest. All the | |
287 | * work happens in the read(), write() and close() routines: */ | |
d7e28ffe RR |
288 | static struct file_operations lguest_fops = { |
289 | .owner = THIS_MODULE, | |
290 | .release = close, | |
291 | .write = write, | |
292 | .read = read, | |
293 | }; | |
dde79789 RR |
294 | |
295 | /* This is a textbook example of a "misc" character device. Populate a "struct | |
296 | * miscdevice" and register it with misc_register(). */ | |
d7e28ffe RR |
297 | static struct miscdevice lguest_dev = { |
298 | .minor = MISC_DYNAMIC_MINOR, | |
299 | .name = "lguest", | |
300 | .fops = &lguest_fops, | |
301 | }; | |
302 | ||
303 | int __init lguest_device_init(void) | |
304 | { | |
305 | return misc_register(&lguest_dev); | |
306 | } | |
307 | ||
308 | void __exit lguest_device_remove(void) | |
309 | { | |
310 | misc_deregister(&lguest_dev); | |
311 | } |