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