diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2009-07-30 18:03:45 -0400 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2009-07-30 02:33:45 -0400 |
commit | 2e04ef76916d1e29a077ea9d0f2003c8fd86724d (patch) | |
tree | 2ff8d625d6e467be9f9f1b67a3674cb6e125e970 /drivers/lguest/lguest_user.c | |
parent | e969fed542cae08cb11d666efac4f7c5d624d09f (diff) |
lguest: fix comment style
I don't really notice it (except to begrudge the extra vertical
space), but Ingo does. And he pointed out that one excuse of lguest
is as a teaching tool, it should set a good example.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Cc: Ingo Molnar <mingo@redhat.com>
Diffstat (limited to 'drivers/lguest/lguest_user.c')
-rw-r--r-- | drivers/lguest/lguest_user.c | 137 |
1 files changed, 90 insertions, 47 deletions
diff --git a/drivers/lguest/lguest_user.c b/drivers/lguest/lguest_user.c index 407722a8e0c4..7e92017103dc 100644 --- a/drivers/lguest/lguest_user.c +++ b/drivers/lguest/lguest_user.c | |||
@@ -1,8 +1,10 @@ | |||
1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher | 1 | /*P:200 |
2 | * This contains all the /dev/lguest code, whereby the userspace launcher | ||
2 | * controls and communicates with the Guest. For example, the first write will | 3 | * controls and communicates with the Guest. For example, the first write will |
3 | * tell us the Guest's memory layout, pagetable, entry point and kernel address | 4 | * 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 | 5 | * offset. A read will run the Guest until something happens, such as a signal |
5 | * or the Guest doing a NOTIFY out to the Launcher. :*/ | 6 | * or the Guest doing a NOTIFY out to the Launcher. |
7 | :*/ | ||
6 | #include <linux/uaccess.h> | 8 | #include <linux/uaccess.h> |
7 | #include <linux/miscdevice.h> | 9 | #include <linux/miscdevice.h> |
8 | #include <linux/fs.h> | 10 | #include <linux/fs.h> |
@@ -37,8 +39,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | |||
37 | if (!addr) | 39 | if (!addr) |
38 | return -EINVAL; | 40 | return -EINVAL; |
39 | 41 | ||
40 | /* Replace the old array with the new one, carefully: others can | 42 | /* |
41 | * be accessing it at the same time */ | 43 | * Replace the old array with the new one, carefully: others can |
44 | * be accessing it at the same time. | ||
45 | */ | ||
42 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), | 46 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
43 | GFP_KERNEL); | 47 | GFP_KERNEL); |
44 | if (!new) | 48 | if (!new) |
@@ -61,8 +65,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | |||
61 | /* Now put new one in place. */ | 65 | /* Now put new one in place. */ |
62 | rcu_assign_pointer(lg->eventfds, new); | 66 | rcu_assign_pointer(lg->eventfds, new); |
63 | 67 | ||
64 | /* We're not in a big hurry. Wait until noone's looking at old | 68 | /* |
65 | * version, then delete it. */ | 69 | * We're not in a big hurry. Wait until noone's looking at old |
70 | * version, then delete it. | ||
71 | */ | ||
66 | synchronize_rcu(); | 72 | synchronize_rcu(); |
67 | kfree(old); | 73 | kfree(old); |
68 | 74 | ||
@@ -87,8 +93,10 @@ static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) | |||
87 | return err; | 93 | return err; |
88 | } | 94 | } |
89 | 95 | ||
90 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt | 96 | /*L:050 |
91 | * number to /dev/lguest. */ | 97 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
98 | * number to /dev/lguest. | ||
99 | */ | ||
92 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | 100 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
93 | { | 101 | { |
94 | unsigned long irq; | 102 | unsigned long irq; |
@@ -102,8 +110,10 @@ static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | |||
102 | return 0; | 110 | return 0; |
103 | } | 111 | } |
104 | 112 | ||
105 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading | 113 | /*L:040 |
106 | * from /dev/lguest. */ | 114 | * Once our Guest is initialized, the Launcher makes it run by reading |
115 | * from /dev/lguest. | ||
116 | */ | ||
107 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | 117 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
108 | { | 118 | { |
109 | struct lguest *lg = file->private_data; | 119 | struct lguest *lg = file->private_data; |
@@ -139,8 +149,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
139 | return len; | 149 | return len; |
140 | } | 150 | } |
141 | 151 | ||
142 | /* If we returned from read() last time because the Guest sent I/O, | 152 | /* |
143 | * clear the flag. */ | 153 | * If we returned from read() last time because the Guest sent I/O, |
154 | * clear the flag. | ||
155 | */ | ||
144 | if (cpu->pending_notify) | 156 | if (cpu->pending_notify) |
145 | cpu->pending_notify = 0; | 157 | cpu->pending_notify = 0; |
146 | 158 | ||
@@ -148,8 +160,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
148 | return run_guest(cpu, (unsigned long __user *)user); | 160 | return run_guest(cpu, (unsigned long __user *)user); |
149 | } | 161 | } |
150 | 162 | ||
151 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only | 163 | /*L:025 |
152 | * uniprocessor, so "id" is always 0. */ | 164 | * This actually initializes a CPU. For the moment, a Guest is only |
165 | * uniprocessor, so "id" is always 0. | ||
166 | */ | ||
153 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | 167 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
154 | { | 168 | { |
155 | /* We have a limited number the number of CPUs in the lguest struct. */ | 169 | /* We have a limited number the number of CPUs in the lguest struct. */ |
@@ -164,8 +178,10 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
164 | /* Each CPU has a timer it can set. */ | 178 | /* Each CPU has a timer it can set. */ |
165 | init_clockdev(cpu); | 179 | init_clockdev(cpu); |
166 | 180 | ||
167 | /* We need a complete page for the Guest registers: they are accessible | 181 | /* |
168 | * to the Guest and we can only grant it access to whole pages. */ | 182 | * We need a complete page for the Guest registers: they are accessible |
183 | * to the Guest and we can only grant it access to whole pages. | ||
184 | */ | ||
169 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); | 185 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
170 | if (!cpu->regs_page) | 186 | if (!cpu->regs_page) |
171 | return -ENOMEM; | 187 | return -ENOMEM; |
@@ -173,29 +189,38 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
173 | /* We actually put the registers at the bottom of the page. */ | 189 | /* We actually put the registers at the bottom of the page. */ |
174 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); | 190 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
175 | 191 | ||
176 | /* Now we initialize the Guest's registers, handing it the start | 192 | /* |
177 | * address. */ | 193 | * Now we initialize the Guest's registers, handing it the start |
194 | * address. | ||
195 | */ | ||
178 | lguest_arch_setup_regs(cpu, start_ip); | 196 | lguest_arch_setup_regs(cpu, start_ip); |
179 | 197 | ||
180 | /* We keep a pointer to the Launcher task (ie. current task) for when | 198 | /* |
181 | * other Guests want to wake this one (eg. console input). */ | 199 | * We keep a pointer to the Launcher task (ie. current task) for when |
200 | * other Guests want to wake this one (eg. console input). | ||
201 | */ | ||
182 | cpu->tsk = current; | 202 | cpu->tsk = current; |
183 | 203 | ||
184 | /* We need to keep a pointer to the Launcher's memory map, because if | 204 | /* |
205 | * We need to keep a pointer to the Launcher's memory map, because if | ||
185 | * the Launcher dies we need to clean it up. If we don't keep a | 206 | * the Launcher dies we need to clean it up. If we don't keep a |
186 | * reference, it is destroyed before close() is called. */ | 207 | * reference, it is destroyed before close() is called. |
208 | */ | ||
187 | cpu->mm = get_task_mm(cpu->tsk); | 209 | cpu->mm = get_task_mm(cpu->tsk); |
188 | 210 | ||
189 | /* We remember which CPU's pages this Guest used last, for optimization | 211 | /* |
190 | * when the same Guest runs on the same CPU twice. */ | 212 | * We remember which CPU's pages this Guest used last, for optimization |
213 | * when the same Guest runs on the same CPU twice. | ||
214 | */ | ||
191 | cpu->last_pages = NULL; | 215 | cpu->last_pages = NULL; |
192 | 216 | ||
193 | /* No error == success. */ | 217 | /* No error == success. */ |
194 | return 0; | 218 | return 0; |
195 | } | 219 | } |
196 | 220 | ||
197 | /*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) | 221 | /*L:020 |
198 | * values (in addition to the LHREQ_INITIALIZE value). These are: | 222 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
223 | * addition to the LHREQ_INITIALIZE value). These are: | ||
199 | * | 224 | * |
200 | * base: The start of the Guest-physical memory inside the Launcher memory. | 225 | * base: The start of the Guest-physical memory inside the Launcher memory. |
201 | * | 226 | * |
@@ -207,14 +232,15 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
207 | */ | 232 | */ |
208 | static int initialize(struct file *file, const unsigned long __user *input) | 233 | static int initialize(struct file *file, const unsigned long __user *input) |
209 | { | 234 | { |
210 | /* "struct lguest" contains everything we (the Host) know about a | 235 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
211 | * Guest. */ | ||
212 | struct lguest *lg; | 236 | struct lguest *lg; |
213 | int err; | 237 | int err; |
214 | unsigned long args[3]; | 238 | unsigned long args[3]; |
215 | 239 | ||
216 | /* We grab the Big Lguest lock, which protects against multiple | 240 | /* |
217 | * simultaneous initializations. */ | 241 | * We grab the Big Lguest lock, which protects against multiple |
242 | * simultaneous initializations. | ||
243 | */ | ||
218 | mutex_lock(&lguest_lock); | 244 | mutex_lock(&lguest_lock); |
219 | /* You can't initialize twice! Close the device and start again... */ | 245 | /* You can't initialize twice! Close the device and start again... */ |
220 | if (file->private_data) { | 246 | if (file->private_data) { |
@@ -249,8 +275,10 @@ static int initialize(struct file *file, const unsigned long __user *input) | |||
249 | if (err) | 275 | if (err) |
250 | goto free_eventfds; | 276 | goto free_eventfds; |
251 | 277 | ||
252 | /* Initialize the Guest's shadow page tables, using the toplevel | 278 | /* |
253 | * address the Launcher gave us. This allocates memory, so can fail. */ | 279 | * Initialize the Guest's shadow page tables, using the toplevel |
280 | * address the Launcher gave us. This allocates memory, so can fail. | ||
281 | */ | ||
254 | err = init_guest_pagetable(lg); | 282 | err = init_guest_pagetable(lg); |
255 | if (err) | 283 | if (err) |
256 | goto free_regs; | 284 | goto free_regs; |
@@ -275,7 +303,8 @@ unlock: | |||
275 | return err; | 303 | return err; |
276 | } | 304 | } |
277 | 305 | ||
278 | /*L:010 The first operation the Launcher does must be a write. All writes | 306 | /*L:010 |
307 | * The first operation the Launcher does must be a write. All writes | ||
279 | * start with an unsigned long number: for the first write this must be | 308 | * start with an unsigned long number: for the first write this must be |
280 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use | 309 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
281 | * writes of other values to send interrupts. | 310 | * writes of other values to send interrupts. |
@@ -283,12 +312,15 @@ unlock: | |||
283 | * Note that we overload the "offset" in the /dev/lguest file to indicate what | 312 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
284 | * CPU number we're dealing with. Currently this is always 0, since we only | 313 | * CPU number we're dealing with. Currently this is always 0, since we only |
285 | * support uniprocessor Guests, but you can see the beginnings of SMP support | 314 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
286 | * here. */ | 315 | * here. |
316 | */ | ||
287 | static ssize_t write(struct file *file, const char __user *in, | 317 | static ssize_t write(struct file *file, const char __user *in, |
288 | size_t size, loff_t *off) | 318 | size_t size, loff_t *off) |
289 | { | 319 | { |
290 | /* Once the Guest is initialized, we hold the "struct lguest" in the | 320 | /* |
291 | * file private data. */ | 321 | * Once the Guest is initialized, we hold the "struct lguest" in the |
322 | * file private data. | ||
323 | */ | ||
292 | struct lguest *lg = file->private_data; | 324 | struct lguest *lg = file->private_data; |
293 | const unsigned long __user *input = (const unsigned long __user *)in; | 325 | const unsigned long __user *input = (const unsigned long __user *)in; |
294 | unsigned long req; | 326 | unsigned long req; |
@@ -323,13 +355,15 @@ static ssize_t write(struct file *file, const char __user *in, | |||
323 | } | 355 | } |
324 | } | 356 | } |
325 | 357 | ||
326 | /*L:060 The final piece of interface code is the close() routine. It reverses | 358 | /*L:060 |
359 | * The final piece of interface code is the close() routine. It reverses | ||
327 | * everything done in initialize(). This is usually called because the | 360 | * everything done in initialize(). This is usually called because the |
328 | * Launcher exited. | 361 | * Launcher exited. |
329 | * | 362 | * |
330 | * Note that the close routine returns 0 or a negative error number: it can't | 363 | * Note that the close routine returns 0 or a negative error number: it can't |
331 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | 364 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
332 | * letting them do it. :*/ | 365 | * letting them do it. |
366 | :*/ | ||
333 | static int close(struct inode *inode, struct file *file) | 367 | static int close(struct inode *inode, struct file *file) |
334 | { | 368 | { |
335 | struct lguest *lg = file->private_data; | 369 | struct lguest *lg = file->private_data; |
@@ -339,8 +373,10 @@ static int close(struct inode *inode, struct file *file) | |||
339 | if (!lg) | 373 | if (!lg) |
340 | return 0; | 374 | return 0; |
341 | 375 | ||
342 | /* We need the big lock, to protect from inter-guest I/O and other | 376 | /* |
343 | * Launchers initializing guests. */ | 377 | * We need the big lock, to protect from inter-guest I/O and other |
378 | * Launchers initializing guests. | ||
379 | */ | ||
344 | mutex_lock(&lguest_lock); | 380 | mutex_lock(&lguest_lock); |
345 | 381 | ||
346 | /* Free up the shadow page tables for the Guest. */ | 382 | /* Free up the shadow page tables for the Guest. */ |
@@ -351,8 +387,10 @@ static int close(struct inode *inode, struct file *file) | |||
351 | hrtimer_cancel(&lg->cpus[i].hrt); | 387 | hrtimer_cancel(&lg->cpus[i].hrt); |
352 | /* We can free up the register page we allocated. */ | 388 | /* We can free up the register page we allocated. */ |
353 | free_page(lg->cpus[i].regs_page); | 389 | free_page(lg->cpus[i].regs_page); |
354 | /* Now all the memory cleanups are done, it's safe to release | 390 | /* |
355 | * the Launcher's memory management structure. */ | 391 | * Now all the memory cleanups are done, it's safe to release |
392 | * the Launcher's memory management structure. | ||
393 | */ | ||
356 | mmput(lg->cpus[i].mm); | 394 | mmput(lg->cpus[i].mm); |
357 | } | 395 | } |
358 | 396 | ||
@@ -361,8 +399,10 @@ static int close(struct inode *inode, struct file *file) | |||
361 | eventfd_ctx_put(lg->eventfds->map[i].event); | 399 | eventfd_ctx_put(lg->eventfds->map[i].event); |
362 | kfree(lg->eventfds); | 400 | kfree(lg->eventfds); |
363 | 401 | ||
364 | /* If lg->dead doesn't contain an error code it will be NULL or a | 402 | /* |
365 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ | 403 | * If lg->dead doesn't contain an error code it will be NULL or a |
404 | * kmalloc()ed string, either of which is ok to hand to kfree(). | ||
405 | */ | ||
366 | if (!IS_ERR(lg->dead)) | 406 | if (!IS_ERR(lg->dead)) |
367 | kfree(lg->dead); | 407 | kfree(lg->dead); |
368 | /* Free the memory allocated to the lguest_struct */ | 408 | /* Free the memory allocated to the lguest_struct */ |
@@ -386,7 +426,8 @@ static int close(struct inode *inode, struct file *file) | |||
386 | * | 426 | * |
387 | * We begin our understanding with the Host kernel interface which the Launcher | 427 | * We begin our understanding with the Host kernel interface which the Launcher |
388 | * uses: reading and writing a character device called /dev/lguest. All the | 428 | * uses: reading and writing a character device called /dev/lguest. All the |
389 | * work happens in the read(), write() and close() routines: */ | 429 | * work happens in the read(), write() and close() routines: |
430 | */ | ||
390 | static struct file_operations lguest_fops = { | 431 | static struct file_operations lguest_fops = { |
391 | .owner = THIS_MODULE, | 432 | .owner = THIS_MODULE, |
392 | .release = close, | 433 | .release = close, |
@@ -394,8 +435,10 @@ static struct file_operations lguest_fops = { | |||
394 | .read = read, | 435 | .read = read, |
395 | }; | 436 | }; |
396 | 437 | ||
397 | /* This is a textbook example of a "misc" character device. Populate a "struct | 438 | /* |
398 | * miscdevice" and register it with misc_register(). */ | 439 | * This is a textbook example of a "misc" character device. Populate a "struct |
440 | * miscdevice" and register it with misc_register(). | ||
441 | */ | ||
399 | static struct miscdevice lguest_dev = { | 442 | static struct miscdevice lguest_dev = { |
400 | .minor = MISC_DYNAMIC_MINOR, | 443 | .minor = MISC_DYNAMIC_MINOR, |
401 | .name = "lguest", | 444 | .name = "lguest", |