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