Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 1 | /*P:200 |
| 2 | * This contains all the /dev/lguest code, whereby the userspace launcher |
Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 3 | * controls and communicates with the Guest. For example, the first write will |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 4 | * tell us the Guest's memory layout, pagetable, entry point and kernel address |
| 5 | * offset. A read will run the Guest until something happens, such as a signal |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 6 | * or the Guest doing a NOTIFY out to the Launcher. |
| 7 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 8 | #include <linux/uaccess.h> |
| 9 | #include <linux/miscdevice.h> |
| 10 | #include <linux/fs.h> |
Glauber de Oliveira Costa | ca94f2b | 2008-01-18 23:59:07 -0200 | [diff] [blame] | 11 | #include <linux/sched.h> |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 12 | #include <linux/eventfd.h> |
| 13 | #include <linux/file.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 14 | #include "lg.h" |
| 15 | |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 16 | bool send_notify_to_eventfd(struct lg_cpu *cpu) |
| 17 | { |
| 18 | unsigned int i; |
| 19 | struct lg_eventfd_map *map; |
| 20 | |
| 21 | /* lg->eventfds is RCU-protected */ |
| 22 | rcu_read_lock(); |
| 23 | map = rcu_dereference(cpu->lg->eventfds); |
| 24 | for (i = 0; i < map->num; i++) { |
| 25 | if (map->map[i].addr == cpu->pending_notify) { |
| 26 | eventfd_signal(map->map[i].event, 1); |
| 27 | cpu->pending_notify = 0; |
| 28 | break; |
| 29 | } |
| 30 | } |
| 31 | rcu_read_unlock(); |
| 32 | return cpu->pending_notify == 0; |
| 33 | } |
| 34 | |
| 35 | static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) |
| 36 | { |
| 37 | struct lg_eventfd_map *new, *old = lg->eventfds; |
| 38 | |
| 39 | if (!addr) |
| 40 | return -EINVAL; |
| 41 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 42 | /* |
| 43 | * Replace the old array with the new one, carefully: others can |
| 44 | * be accessing it at the same time. |
| 45 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 46 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
| 47 | GFP_KERNEL); |
| 48 | if (!new) |
| 49 | return -ENOMEM; |
| 50 | |
| 51 | /* First make identical copy. */ |
| 52 | memcpy(new->map, old->map, sizeof(old->map[0]) * old->num); |
| 53 | new->num = old->num; |
| 54 | |
| 55 | /* Now append new entry. */ |
| 56 | new->map[new->num].addr = addr; |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 57 | new->map[new->num].event = eventfd_ctx_fdget(fd); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 58 | if (IS_ERR(new->map[new->num].event)) { |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 59 | int err = PTR_ERR(new->map[new->num].event); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 60 | kfree(new); |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 61 | return err; |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 62 | } |
| 63 | new->num++; |
| 64 | |
| 65 | /* Now put new one in place. */ |
| 66 | rcu_assign_pointer(lg->eventfds, new); |
| 67 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 68 | /* |
| 69 | * We're not in a big hurry. Wait until noone's looking at old |
| 70 | * version, then delete it. |
| 71 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 72 | synchronize_rcu(); |
| 73 | kfree(old); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) |
| 79 | { |
| 80 | unsigned long addr, fd; |
| 81 | int err; |
| 82 | |
| 83 | if (get_user(addr, input) != 0) |
| 84 | return -EFAULT; |
| 85 | input++; |
| 86 | if (get_user(fd, input) != 0) |
| 87 | return -EFAULT; |
| 88 | |
| 89 | mutex_lock(&lguest_lock); |
| 90 | err = add_eventfd(lg, addr, fd); |
| 91 | mutex_unlock(&lguest_lock); |
| 92 | |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 93 | return err; |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 94 | } |
| 95 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 96 | /*L:050 |
| 97 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 98 | * number to /dev/lguest. |
| 99 | */ |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 100 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 101 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 102 | unsigned long irq; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 103 | |
| 104 | if (get_user(irq, input) != 0) |
| 105 | return -EFAULT; |
| 106 | if (irq >= LGUEST_IRQS) |
| 107 | return -EINVAL; |
Rusty Russell | 9f155a9 | 2009-06-12 22:27:08 -0600 | [diff] [blame] | 108 | |
| 109 | set_interrupt(cpu, irq); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 113 | /*L:040 |
| 114 | * Once our Guest is initialized, the Launcher makes it run by reading |
| 115 | * from /dev/lguest. |
| 116 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 117 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 118 | { |
| 119 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 120 | struct lg_cpu *cpu; |
| 121 | unsigned int cpu_id = *o; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 122 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 123 | /* You must write LHREQ_INITIALIZE first! */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 124 | if (!lg) |
| 125 | return -EINVAL; |
| 126 | |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 127 | /* Watch out for arbitrary vcpu indexes! */ |
| 128 | if (cpu_id >= lg->nr_cpus) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | cpu = &lg->cpus[cpu_id]; |
| 132 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 133 | /* If you're not the task which owns the Guest, go away. */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 134 | if (current != cpu->tsk) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 135 | return -EPERM; |
| 136 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 137 | /* If the Guest is already dead, we indicate why */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 138 | if (lg->dead) { |
| 139 | size_t len; |
| 140 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 141 | /* lg->dead either contains an error code, or a string. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 142 | if (IS_ERR(lg->dead)) |
| 143 | return PTR_ERR(lg->dead); |
| 144 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 145 | /* We can only return as much as the buffer they read with. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 146 | len = min(size, strlen(lg->dead)+1); |
| 147 | if (copy_to_user(user, lg->dead, len) != 0) |
| 148 | return -EFAULT; |
| 149 | return len; |
| 150 | } |
| 151 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 152 | /* |
| 153 | * If we returned from read() last time because the Guest sent I/O, |
| 154 | * clear the flag. |
| 155 | */ |
Glauber de Oliveira Costa | 5e232f4 | 2008-01-07 11:05:36 -0200 | [diff] [blame] | 156 | if (cpu->pending_notify) |
| 157 | cpu->pending_notify = 0; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 158 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 159 | /* Run the Guest until something interesting happens. */ |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 160 | return run_guest(cpu, (unsigned long __user *)user); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 163 | /*L:025 |
| 164 | * This actually initializes a CPU. For the moment, a Guest is only |
| 165 | * uniprocessor, so "id" is always 0. |
| 166 | */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 167 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 168 | { |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 169 | /* We have a limited number the number of CPUs in the lguest struct. */ |
Rusty Russell | 24adf12 | 2008-05-02 21:50:51 -0500 | [diff] [blame] | 170 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 173 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 174 | cpu->id = id; |
| 175 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); |
| 176 | cpu->lg->nr_cpus++; |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 177 | |
| 178 | /* Each CPU has a timer it can set. */ |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 179 | init_clockdev(cpu); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 180 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 181 | /* |
| 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 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 185 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
| 186 | if (!cpu->regs_page) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | /* We actually put the registers at the bottom of the page. */ |
| 190 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 191 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 192 | /* |
| 193 | * Now we initialize the Guest's registers, handing it the start |
| 194 | * address. |
| 195 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 196 | lguest_arch_setup_regs(cpu, start_ip); |
| 197 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 198 | /* |
| 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 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 202 | cpu->tsk = current; |
| 203 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 204 | /* |
| 205 | * We need to keep a pointer to the Launcher's memory map, because if |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 206 | * the Launcher dies we need to clean it up. If we don't keep a |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 207 | * reference, it is destroyed before close() is called. |
| 208 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 209 | cpu->mm = get_task_mm(cpu->tsk); |
| 210 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 211 | /* |
| 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 | */ |
Glauber de Oliveira Costa | f34f8c5 | 2008-01-17 19:13:26 -0200 | [diff] [blame] | 215 | cpu->last_pages = NULL; |
| 216 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 217 | /* No error == success. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 221 | /*L:020 |
| 222 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
| 223 | * addition to the LHREQ_INITIALIZE value). These are: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 224 | * |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 225 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 226 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 227 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 228 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
| 229 | * this to ensure the Guest can only reach its own memory. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 230 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 231 | * start: The first instruction to execute ("eip" in x86-speak). |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 232 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 233 | static int initialize(struct file *file, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 234 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 235 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 236 | struct lguest *lg; |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 237 | int err; |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 238 | unsigned long args[3]; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 239 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 240 | /* |
| 241 | * We grab the Big Lguest lock, which protects against multiple |
| 242 | * simultaneous initializations. |
| 243 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 244 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 245 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 246 | if (file->private_data) { |
| 247 | err = -EBUSY; |
| 248 | goto unlock; |
| 249 | } |
| 250 | |
| 251 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 252 | err = -EFAULT; |
| 253 | goto unlock; |
| 254 | } |
| 255 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 256 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
| 257 | if (!lg) { |
| 258 | err = -ENOMEM; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 259 | goto unlock; |
| 260 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 261 | |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 262 | lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL); |
| 263 | if (!lg->eventfds) { |
| 264 | err = -ENOMEM; |
| 265 | goto free_lg; |
| 266 | } |
| 267 | lg->eventfds->num = 0; |
| 268 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 269 | /* Populate the easy fields of our "struct lguest" */ |
Al Viro | 74dbf71 | 2008-03-29 03:08:28 +0000 | [diff] [blame] | 270 | lg->mem_base = (void __user *)args[0]; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 271 | lg->pfn_limit = args[1]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 272 | |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 273 | /* This is the first cpu (cpu 0) and it will start booting at args[2] */ |
| 274 | err = lg_cpu_start(&lg->cpus[0], 0, args[2]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 275 | if (err) |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 276 | goto free_eventfds; |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 277 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 278 | /* |
| 279 | * Initialize the Guest's shadow page tables, using the toplevel |
| 280 | * address the Launcher gave us. This allocates memory, so can fail. |
| 281 | */ |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 282 | err = init_guest_pagetable(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 283 | if (err) |
| 284 | goto free_regs; |
| 285 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 286 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 287 | file->private_data = lg; |
| 288 | |
| 289 | mutex_unlock(&lguest_lock); |
| 290 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 291 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 292 | return sizeof(args); |
| 293 | |
| 294 | free_regs: |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 295 | /* FIXME: This should be in free_vcpu */ |
| 296 | free_page(lg->cpus[0].regs_page); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 297 | free_eventfds: |
| 298 | kfree(lg->eventfds); |
| 299 | free_lg: |
Adrian Bunk | 4305441 | 2007-11-14 16:59:00 -0800 | [diff] [blame] | 300 | kfree(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 301 | unlock: |
| 302 | mutex_unlock(&lguest_lock); |
| 303 | return err; |
| 304 | } |
| 305 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 306 | /*L:010 |
| 307 | * The first operation the Launcher does must be a write. All writes |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 308 | * start with an unsigned long number: for the first write this must be |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 309 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 310 | * writes of other values to send interrupts. |
| 311 | * |
| 312 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
| 313 | * CPU number we're dealing with. Currently this is always 0, since we only |
| 314 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 315 | * here. |
| 316 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 317 | static ssize_t write(struct file *file, const char __user *in, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 318 | size_t size, loff_t *off) |
| 319 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 320 | /* |
| 321 | * Once the Guest is initialized, we hold the "struct lguest" in the |
| 322 | * file private data. |
| 323 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 324 | struct lguest *lg = file->private_data; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 325 | const unsigned long __user *input = (const unsigned long __user *)in; |
| 326 | unsigned long req; |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 327 | struct lg_cpu *uninitialized_var(cpu); |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 328 | unsigned int cpu_id = *off; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 329 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 330 | /* The first value tells us what this request is. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 331 | if (get_user(req, input) != 0) |
| 332 | return -EFAULT; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 333 | input++; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 334 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 335 | /* If you haven't initialized, you must do that first. */ |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 336 | if (req != LHREQ_INITIALIZE) { |
| 337 | if (!lg || (cpu_id >= lg->nr_cpus)) |
| 338 | return -EINVAL; |
| 339 | cpu = &lg->cpus[cpu_id]; |
Eugene Teo | f73d1e6 | 2008-02-09 23:53:17 +0800 | [diff] [blame] | 340 | |
| 341 | /* Once the Guest is dead, you can only read() why it died. */ |
| 342 | if (lg->dead) |
| 343 | return -ENOENT; |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 344 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 345 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 346 | switch (req) { |
| 347 | case LHREQ_INITIALIZE: |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 348 | return initialize(file, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 349 | case LHREQ_IRQ: |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 350 | return user_send_irq(cpu, input); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 351 | case LHREQ_EVENTFD: |
| 352 | return attach_eventfd(lg, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 353 | default: |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | } |
| 357 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 358 | /*L:060 |
| 359 | * The final piece of interface code is the close() routine. It reverses |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 360 | * everything done in initialize(). This is usually called because the |
| 361 | * Launcher exited. |
| 362 | * |
| 363 | * Note that the close routine returns 0 or a negative error number: it can't |
| 364 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 365 | * letting them do it. |
| 366 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 367 | static int close(struct inode *inode, struct file *file) |
| 368 | { |
| 369 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 370 | unsigned int i; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 371 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 372 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 373 | if (!lg) |
| 374 | return 0; |
| 375 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 376 | /* |
| 377 | * We need the big lock, to protect from inter-guest I/O and other |
| 378 | * Launchers initializing guests. |
| 379 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 380 | mutex_lock(&lguest_lock); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 381 | |
| 382 | /* Free up the shadow page tables for the Guest. */ |
| 383 | free_guest_pagetable(lg); |
| 384 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 385 | for (i = 0; i < lg->nr_cpus; i++) { |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 386 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 387 | hrtimer_cancel(&lg->cpus[i].hrt); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 388 | /* We can free up the register page we allocated. */ |
| 389 | free_page(lg->cpus[i].regs_page); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 390 | /* |
| 391 | * Now all the memory cleanups are done, it's safe to release |
| 392 | * the Launcher's memory management structure. |
| 393 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 394 | mmput(lg->cpus[i].mm); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 395 | } |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 396 | |
| 397 | /* Release any eventfds they registered. */ |
| 398 | for (i = 0; i < lg->eventfds->num; i++) |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 399 | eventfd_ctx_put(lg->eventfds->map[i].event); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 400 | kfree(lg->eventfds); |
| 401 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 402 | /* |
| 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 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 406 | if (!IS_ERR(lg->dead)) |
| 407 | kfree(lg->dead); |
Mark Wallis | 05dfdbb | 2009-01-26 17:32:35 +1100 | [diff] [blame] | 408 | /* Free the memory allocated to the lguest_struct */ |
| 409 | kfree(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 410 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 411 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 412 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 416 | /*L:000 |
| 417 | * Welcome to our journey through the Launcher! |
| 418 | * |
| 419 | * The Launcher is the Host userspace program which sets up, runs and services |
| 420 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" |
| 421 | * doing things are inaccurate: the Launcher does all the device handling for |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 422 | * the Guest, but the Guest can't know that. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 423 | * |
| 424 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 425 | * shall see more of that later. |
| 426 | * |
| 427 | * We begin our understanding with the Host kernel interface which the Launcher |
| 428 | * uses: reading and writing a character device called /dev/lguest. All the |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 429 | * work happens in the read(), write() and close() routines: |
| 430 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 431 | static struct file_operations lguest_fops = { |
| 432 | .owner = THIS_MODULE, |
| 433 | .release = close, |
| 434 | .write = write, |
| 435 | .read = read, |
| 436 | }; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 437 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame^] | 438 | /* |
| 439 | * This is a textbook example of a "misc" character device. Populate a "struct |
| 440 | * miscdevice" and register it with misc_register(). |
| 441 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 442 | static struct miscdevice lguest_dev = { |
| 443 | .minor = MISC_DYNAMIC_MINOR, |
| 444 | .name = "lguest", |
| 445 | .fops = &lguest_fops, |
| 446 | }; |
| 447 | |
| 448 | int __init lguest_device_init(void) |
| 449 | { |
| 450 | return misc_register(&lguest_dev); |
| 451 | } |
| 452 | |
| 453 | void __exit lguest_device_remove(void) |
| 454 | { |
| 455 | misc_deregister(&lguest_dev); |
| 456 | } |