Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) |
| 3 | * Copyright (C) 2001 - 2003 Jeff Dike (jdike@addtoit.com) |
| 4 | * Licensed under the GPL |
| 5 | */ |
| 6 | |
| 7 | #include "linux/kernel.h" |
| 8 | #include "linux/slab.h" |
| 9 | #include "linux/init.h" |
| 10 | #include "linux/notifier.h" |
| 11 | #include "linux/reboot.h" |
| 12 | #include "linux/utsname.h" |
| 13 | #include "linux/ctype.h" |
| 14 | #include "linux/interrupt.h" |
| 15 | #include "linux/sysrq.h" |
| 16 | #include "linux/workqueue.h" |
| 17 | #include "linux/module.h" |
| 18 | #include "linux/file.h" |
| 19 | #include "linux/fs.h" |
| 20 | #include "linux/namei.h" |
| 21 | #include "linux/proc_fs.h" |
| 22 | #include "linux/syscalls.h" |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 23 | #include "linux/list.h" |
| 24 | #include "linux/mm.h" |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 25 | #include "linux/console.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "asm/irq.h" |
| 27 | #include "asm/uaccess.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "kern_util.h" |
| 29 | #include "kern.h" |
| 30 | #include "mconsole.h" |
| 31 | #include "mconsole_kern.h" |
| 32 | #include "irq_user.h" |
| 33 | #include "init.h" |
| 34 | #include "os.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "irq_kern.h" |
| 36 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 37 | static int do_unlink_socket(struct notifier_block *notifier, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | unsigned long what, void *data) |
| 39 | { |
| 40 | return(mconsole_unlink_socket()); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static struct notifier_block reboot_notifier = { |
| 45 | .notifier_call = do_unlink_socket, |
| 46 | .priority = 0, |
| 47 | }; |
| 48 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 49 | /* Safe without explicit locking for now. Tasklets provide their own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * locking, and the interrupt handler is safe because it can't interrupt |
| 51 | * itself and it can only happen on CPU 0. |
| 52 | */ |
| 53 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 54 | static LIST_HEAD(mc_requests); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 56 | static void mc_work_proc(struct work_struct *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | struct mconsole_entry *req; |
| 59 | unsigned long flags; |
| 60 | |
| 61 | while(!list_empty(&mc_requests)){ |
Paolo 'Blaisorblade' Giarrusso | dbdb4c0 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 62 | local_irq_save(flags); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 63 | req = list_entry(mc_requests.next, struct mconsole_entry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | list); |
| 65 | list_del(&req->list); |
| 66 | local_irq_restore(flags); |
| 67 | req->request.cmd->handler(&req->request); |
| 68 | kfree(req); |
| 69 | } |
| 70 | } |
| 71 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 72 | static DECLARE_WORK(mconsole_work, mc_work_proc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 74 | static irqreturn_t mconsole_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | /* long to avoid size mismatch warnings from gcc */ |
| 77 | long fd; |
| 78 | struct mconsole_entry *new; |
Al Viro | 3a51237 | 2006-10-24 11:15:29 +0100 | [diff] [blame] | 79 | static struct mc_request req; /* that's OK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | fd = (long) dev_id; |
| 82 | while (mconsole_get_request(fd, &req)){ |
| 83 | if(req.cmd->context == MCONSOLE_INTR) |
| 84 | (*req.cmd->handler)(&req); |
| 85 | else { |
Jeff Dike | 60baa15 | 2006-04-10 22:53:28 -0700 | [diff] [blame] | 86 | new = kmalloc(sizeof(*new), GFP_NOWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if(new == NULL) |
| 88 | mconsole_reply(&req, "Out of memory", 1, 0); |
| 89 | else { |
| 90 | new->request = req; |
Al Viro | 3a51237 | 2006-10-24 11:15:29 +0100 | [diff] [blame] | 91 | new->request.regs = get_irq_regs()->regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | list_add(&new->list, &mc_requests); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | if(!list_empty(&mc_requests)) |
| 97 | schedule_work(&mconsole_work); |
| 98 | reactivate_fd(fd, MCONSOLE_IRQ); |
| 99 | return(IRQ_HANDLED); |
| 100 | } |
| 101 | |
| 102 | void mconsole_version(struct mc_request *req) |
| 103 | { |
| 104 | char version[256]; |
| 105 | |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 106 | sprintf(version, "%s %s %s %s %s", utsname()->sysname, |
| 107 | utsname()->nodename, utsname()->release, |
| 108 | utsname()->version, utsname()->machine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | mconsole_reply(req, version, 0, 0); |
| 110 | } |
| 111 | |
| 112 | void mconsole_log(struct mc_request *req) |
| 113 | { |
| 114 | int len; |
| 115 | char *ptr = req->request.data; |
| 116 | |
| 117 | ptr += strlen("log "); |
| 118 | |
| 119 | len = req->len - (ptr - req->request.data); |
| 120 | printk("%.*s", len, ptr); |
| 121 | mconsole_reply(req, "", 0, 0); |
| 122 | } |
| 123 | |
| 124 | /* This is a more convoluted version of mconsole_proc, which has some stability |
| 125 | * problems; however, we need it fixed, because it is expected that UML users |
| 126 | * mount HPPFS instead of procfs on /proc. And we want mconsole_proc to still |
| 127 | * show the real procfs content, not the ones from hppfs.*/ |
| 128 | #if 0 |
| 129 | void mconsole_proc(struct mc_request *req) |
| 130 | { |
| 131 | struct nameidata nd; |
| 132 | struct file_system_type *proc; |
| 133 | struct super_block *super; |
| 134 | struct file *file; |
| 135 | int n, err; |
| 136 | char *ptr = req->request.data, *buf; |
| 137 | |
| 138 | ptr += strlen("proc"); |
| 139 | while(isspace(*ptr)) ptr++; |
| 140 | |
| 141 | proc = get_fs_type("proc"); |
| 142 | if(proc == NULL){ |
| 143 | mconsole_reply(req, "procfs not registered", 1, 0); |
| 144 | goto out; |
| 145 | } |
| 146 | |
| 147 | super = (*proc->get_sb)(proc, 0, NULL, NULL); |
| 148 | put_filesystem(proc); |
| 149 | if(super == NULL){ |
| 150 | mconsole_reply(req, "Failed to get procfs superblock", 1, 0); |
| 151 | goto out; |
| 152 | } |
| 153 | up_write(&super->s_umount); |
| 154 | |
| 155 | nd.dentry = super->s_root; |
| 156 | nd.mnt = NULL; |
| 157 | nd.flags = O_RDONLY + 1; |
| 158 | nd.last_type = LAST_ROOT; |
| 159 | |
| 160 | /* START: it was experienced that the stability problems are closed |
| 161 | * if commenting out these two calls + the below read cycle. To |
| 162 | * make UML crash again, it was enough to readd either one.*/ |
| 163 | err = link_path_walk(ptr, &nd); |
| 164 | if(err){ |
| 165 | mconsole_reply(req, "Failed to look up file", 1, 0); |
| 166 | goto out_kill; |
| 167 | } |
| 168 | |
| 169 | file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); |
| 170 | if(IS_ERR(file)){ |
| 171 | mconsole_reply(req, "Failed to open file", 1, 0); |
| 172 | goto out_kill; |
| 173 | } |
| 174 | /*END*/ |
| 175 | |
| 176 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 177 | if(buf == NULL){ |
| 178 | mconsole_reply(req, "Failed to allocate buffer", 1, 0); |
| 179 | goto out_fput; |
| 180 | } |
| 181 | |
| 182 | if((file->f_op != NULL) && (file->f_op->read != NULL)){ |
| 183 | do { |
| 184 | n = (*file->f_op->read)(file, buf, PAGE_SIZE - 1, |
| 185 | &file->f_pos); |
| 186 | if(n >= 0){ |
| 187 | buf[n] = '\0'; |
| 188 | mconsole_reply(req, buf, 0, (n > 0)); |
| 189 | } |
| 190 | else { |
| 191 | mconsole_reply(req, "Read of file failed", |
| 192 | 1, 0); |
| 193 | goto out_free; |
| 194 | } |
| 195 | } while(n > 0); |
| 196 | } |
| 197 | else mconsole_reply(req, "", 0, 0); |
| 198 | |
| 199 | out_free: |
| 200 | kfree(buf); |
| 201 | out_fput: |
| 202 | fput(file); |
| 203 | out_kill: |
| 204 | deactivate_super(super); |
| 205 | out: ; |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | void mconsole_proc(struct mc_request *req) |
| 210 | { |
| 211 | char path[64]; |
| 212 | char *buf; |
| 213 | int len; |
| 214 | int fd; |
| 215 | int first_chunk = 1; |
| 216 | char *ptr = req->request.data; |
| 217 | |
| 218 | ptr += strlen("proc"); |
| 219 | while(isspace(*ptr)) ptr++; |
| 220 | snprintf(path, sizeof(path), "/proc/%s", ptr); |
| 221 | |
| 222 | fd = sys_open(path, 0, 0); |
| 223 | if (fd < 0) { |
| 224 | mconsole_reply(req, "Failed to open file", 1, 0); |
| 225 | printk("open %s: %d\n",path,fd); |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 230 | if(buf == NULL){ |
| 231 | mconsole_reply(req, "Failed to allocate buffer", 1, 0); |
| 232 | goto out_close; |
| 233 | } |
| 234 | |
| 235 | for (;;) { |
| 236 | len = sys_read(fd, buf, PAGE_SIZE-1); |
| 237 | if (len < 0) { |
| 238 | mconsole_reply(req, "Read of file failed", 1, 0); |
| 239 | goto out_free; |
| 240 | } |
| 241 | /*Begin the file content on his own line.*/ |
| 242 | if (first_chunk) { |
| 243 | mconsole_reply(req, "\n", 0, 1); |
| 244 | first_chunk = 0; |
| 245 | } |
| 246 | if (len == PAGE_SIZE-1) { |
| 247 | buf[len] = '\0'; |
| 248 | mconsole_reply(req, buf, 0, 1); |
| 249 | } else { |
| 250 | buf[len] = '\0'; |
| 251 | mconsole_reply(req, buf, 0, 0); |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | out_free: |
| 257 | kfree(buf); |
| 258 | out_close: |
| 259 | sys_close(fd); |
| 260 | out: |
| 261 | /* nothing */; |
| 262 | } |
| 263 | |
| 264 | #define UML_MCONSOLE_HELPTEXT \ |
| 265 | "Commands: \n\ |
| 266 | version - Get kernel version \n\ |
| 267 | help - Print this message \n\ |
| 268 | halt - Halt UML \n\ |
| 269 | reboot - Reboot UML \n\ |
| 270 | config <dev>=<config> - Add a new device to UML; \n\ |
| 271 | same syntax as command line \n\ |
| 272 | config <dev> - Query the configuration of a device \n\ |
| 273 | remove <dev> - Remove a device from UML \n\ |
| 274 | sysrq <letter> - Performs the SysRq action controlled by the letter \n\ |
Jeff Dike | db80581 | 2006-02-01 03:06:23 -0800 | [diff] [blame] | 275 | cad - invoke the Ctrl-Alt-Del handler \n\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | stop - pause the UML; it will do nothing until it receives a 'go' \n\ |
| 277 | go - continue the UML after a 'stop' \n\ |
| 278 | log <string> - make UML enter <string> into the kernel log\n\ |
| 279 | proc <file> - returns the contents of the UML's /proc/<file>\n\ |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 280 | stack <pid> - returns the stack of the specified pid\n\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | " |
| 282 | |
| 283 | void mconsole_help(struct mc_request *req) |
| 284 | { |
| 285 | mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0); |
| 286 | } |
| 287 | |
| 288 | void mconsole_halt(struct mc_request *req) |
| 289 | { |
| 290 | mconsole_reply(req, "", 0, 0); |
| 291 | machine_halt(); |
| 292 | } |
| 293 | |
| 294 | void mconsole_reboot(struct mc_request *req) |
| 295 | { |
| 296 | mconsole_reply(req, "", 0, 0); |
| 297 | machine_restart(NULL); |
| 298 | } |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | void mconsole_cad(struct mc_request *req) |
| 301 | { |
| 302 | mconsole_reply(req, "", 0, 0); |
| 303 | ctrl_alt_del(); |
| 304 | } |
| 305 | |
| 306 | void mconsole_go(struct mc_request *req) |
| 307 | { |
| 308 | mconsole_reply(req, "Not stopped", 1, 0); |
| 309 | } |
| 310 | |
| 311 | void mconsole_stop(struct mc_request *req) |
| 312 | { |
| 313 | deactivate_fd(req->originating_fd, MCONSOLE_IRQ); |
| 314 | os_set_fd_block(req->originating_fd, 1); |
Al Viro | 3a51237 | 2006-10-24 11:15:29 +0100 | [diff] [blame] | 315 | mconsole_reply(req, "stopped", 0, 0); |
| 316 | while (mconsole_get_request(req->originating_fd, req)) { |
| 317 | if (req->cmd->handler == mconsole_go) |
| 318 | break; |
| 319 | if (req->cmd->handler == mconsole_stop) { |
| 320 | mconsole_reply(req, "Already stopped", 1, 0); |
| 321 | continue; |
| 322 | } |
| 323 | if (req->cmd->handler == mconsole_sysrq) { |
| 324 | struct pt_regs *old_regs; |
| 325 | old_regs = set_irq_regs((struct pt_regs *)&req->regs); |
| 326 | mconsole_sysrq(req); |
| 327 | set_irq_regs(old_regs); |
| 328 | continue; |
| 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | (*req->cmd->handler)(req); |
| 331 | } |
| 332 | os_set_fd_block(req->originating_fd, 0); |
| 333 | reactivate_fd(req->originating_fd, MCONSOLE_IRQ); |
| 334 | mconsole_reply(req, "", 0, 0); |
| 335 | } |
| 336 | |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 337 | static DEFINE_SPINLOCK(mc_devices_lock); |
Paolo 'Blaisorblade' Giarrusso | 42947cb | 2006-02-01 03:06:29 -0800 | [diff] [blame] | 338 | static LIST_HEAD(mconsole_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | void mconsole_register_dev(struct mc_device *new) |
| 341 | { |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 342 | spin_lock(&mc_devices_lock); |
| 343 | BUG_ON(!list_empty(&new->list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | list_add(&new->list, &mconsole_devices); |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 345 | spin_unlock(&mc_devices_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static struct mc_device *mconsole_find_dev(char *name) |
| 349 | { |
| 350 | struct list_head *ele; |
| 351 | struct mc_device *dev; |
| 352 | |
| 353 | list_for_each(ele, &mconsole_devices){ |
| 354 | dev = list_entry(ele, struct mc_device, list); |
| 355 | if(!strncmp(name, dev->name, strlen(dev->name))) |
| 356 | return(dev); |
| 357 | } |
| 358 | return(NULL); |
| 359 | } |
| 360 | |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 361 | #define UNPLUGGED_PER_PAGE \ |
| 362 | ((PAGE_SIZE - sizeof(struct list_head)) / sizeof(unsigned long)) |
| 363 | |
| 364 | struct unplugged_pages { |
| 365 | struct list_head list; |
| 366 | void *pages[UNPLUGGED_PER_PAGE]; |
| 367 | }; |
| 368 | |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 369 | static DECLARE_MUTEX(plug_mem_mutex); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 370 | static unsigned long long unplugged_pages_count = 0; |
Jeff Dike | c59bce6 | 2007-02-10 01:44:04 -0800 | [diff] [blame] | 371 | static LIST_HEAD(unplugged_pages); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 372 | static int unplug_index = UNPLUGGED_PER_PAGE; |
| 373 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 374 | static int mem_config(char *str, char **error_out) |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 375 | { |
| 376 | unsigned long long diff; |
| 377 | int err = -EINVAL, i, add; |
| 378 | char *ret; |
| 379 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 380 | if(str[0] != '='){ |
| 381 | *error_out = "Expected '=' after 'mem'"; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 382 | goto out; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 383 | } |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 384 | |
| 385 | str++; |
| 386 | if(str[0] == '-') |
| 387 | add = 0; |
| 388 | else if(str[0] == '+'){ |
| 389 | add = 1; |
| 390 | } |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 391 | else { |
| 392 | *error_out = "Expected increment to start with '-' or '+'"; |
| 393 | goto out; |
| 394 | } |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 395 | |
| 396 | str++; |
| 397 | diff = memparse(str, &ret); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 398 | if(*ret != '\0'){ |
| 399 | *error_out = "Failed to parse memory increment"; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 400 | goto out; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 401 | } |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 402 | |
| 403 | diff /= PAGE_SIZE; |
| 404 | |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 405 | down(&plug_mem_mutex); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 406 | for(i = 0; i < diff; i++){ |
| 407 | struct unplugged_pages *unplugged; |
| 408 | void *addr; |
| 409 | |
| 410 | if(add){ |
| 411 | if(list_empty(&unplugged_pages)) |
| 412 | break; |
| 413 | |
| 414 | unplugged = list_entry(unplugged_pages.next, |
| 415 | struct unplugged_pages, list); |
| 416 | if(unplug_index > 0) |
| 417 | addr = unplugged->pages[--unplug_index]; |
| 418 | else { |
| 419 | list_del(&unplugged->list); |
| 420 | addr = unplugged; |
| 421 | unplug_index = UNPLUGGED_PER_PAGE; |
| 422 | } |
| 423 | |
| 424 | free_page((unsigned long) addr); |
| 425 | unplugged_pages_count--; |
| 426 | } |
| 427 | else { |
| 428 | struct page *page; |
| 429 | |
| 430 | page = alloc_page(GFP_ATOMIC); |
| 431 | if(page == NULL) |
| 432 | break; |
| 433 | |
| 434 | unplugged = page_address(page); |
| 435 | if(unplug_index == UNPLUGGED_PER_PAGE){ |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 436 | list_add(&unplugged->list, &unplugged_pages); |
| 437 | unplug_index = 0; |
| 438 | } |
| 439 | else { |
| 440 | struct list_head *entry = unplugged_pages.next; |
| 441 | addr = unplugged; |
| 442 | |
| 443 | unplugged = list_entry(entry, |
| 444 | struct unplugged_pages, |
| 445 | list); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 446 | err = os_drop_memory(addr, PAGE_SIZE); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 447 | if(err){ |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 448 | printk("Failed to release memory - " |
| 449 | "errno = %d\n", err); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 450 | *error_out = "Failed to release memory"; |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 451 | goto out_unlock; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 452 | } |
| 453 | unplugged->pages[unplug_index++] = addr; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | unplugged_pages_count++; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | err = 0; |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 461 | out_unlock: |
| 462 | up(&plug_mem_mutex); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 463 | out: |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | static int mem_get_config(char *name, char *str, int size, char **error_out) |
| 468 | { |
| 469 | char buf[sizeof("18446744073709551615")]; |
| 470 | int len = 0; |
| 471 | |
| 472 | sprintf(buf, "%ld", uml_physmem); |
| 473 | CONFIG_CHUNK(str, size, len, buf, 1); |
| 474 | |
| 475 | return len; |
| 476 | } |
| 477 | |
| 478 | static int mem_id(char **str, int *start_out, int *end_out) |
| 479 | { |
| 480 | *start_out = 0; |
| 481 | *end_out = 0; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 486 | static int mem_remove(int n, char **error_out) |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 487 | { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 488 | *error_out = "Memory doesn't support the remove operation"; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 489 | return -EBUSY; |
| 490 | } |
| 491 | |
| 492 | static struct mc_device mem_mc = { |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 493 | .list = LIST_HEAD_INIT(mem_mc.list), |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 494 | .name = "mem", |
| 495 | .config = mem_config, |
| 496 | .get_config = mem_get_config, |
| 497 | .id = mem_id, |
| 498 | .remove = mem_remove, |
| 499 | }; |
| 500 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 501 | static int __init mem_mc_init(void) |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 502 | { |
| 503 | if(can_drop_memory()) |
| 504 | mconsole_register_dev(&mem_mc); |
| 505 | else printk("Can't release memory to the host - memory hotplug won't " |
| 506 | "be supported\n"); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | __initcall(mem_mc_init); |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | #define CONFIG_BUF_SIZE 64 |
| 513 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 514 | static void mconsole_get_config(int (*get_config)(char *, char *, int, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | char **), |
| 516 | struct mc_request *req, char *name) |
| 517 | { |
| 518 | char default_buf[CONFIG_BUF_SIZE], *error, *buf; |
| 519 | int n, size; |
| 520 | |
| 521 | if(get_config == NULL){ |
| 522 | mconsole_reply(req, "No get_config routine defined", 1, 0); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | error = NULL; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 527 | size = ARRAY_SIZE(default_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | buf = default_buf; |
| 529 | |
| 530 | while(1){ |
| 531 | n = (*get_config)(name, buf, size, &error); |
| 532 | if(error != NULL){ |
| 533 | mconsole_reply(req, error, 1, 0); |
| 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | if(n <= size){ |
| 538 | mconsole_reply(req, buf, 0, 0); |
| 539 | goto out; |
| 540 | } |
| 541 | |
| 542 | if(buf != default_buf) |
| 543 | kfree(buf); |
| 544 | |
| 545 | size = n; |
| 546 | buf = kmalloc(size, GFP_KERNEL); |
| 547 | if(buf == NULL){ |
| 548 | mconsole_reply(req, "Failed to allocate buffer", 1, 0); |
| 549 | return; |
| 550 | } |
| 551 | } |
| 552 | out: |
| 553 | if(buf != default_buf) |
| 554 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void mconsole_config(struct mc_request *req) |
| 558 | { |
| 559 | struct mc_device *dev; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 560 | char *ptr = req->request.data, *name, *error_string = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | int err; |
| 562 | |
| 563 | ptr += strlen("config"); |
| 564 | while(isspace(*ptr)) ptr++; |
| 565 | dev = mconsole_find_dev(ptr); |
| 566 | if(dev == NULL){ |
| 567 | mconsole_reply(req, "Bad configuration option", 1, 0); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | name = &ptr[strlen(dev->name)]; |
| 572 | ptr = name; |
| 573 | while((*ptr != '=') && (*ptr != '\0')) |
| 574 | ptr++; |
| 575 | |
| 576 | if(*ptr == '='){ |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 577 | err = (*dev->config)(name, &error_string); |
| 578 | mconsole_reply(req, error_string, err, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | else mconsole_get_config(dev->get_config, req, name); |
| 581 | } |
| 582 | |
| 583 | void mconsole_remove(struct mc_request *req) |
| 584 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 585 | struct mc_device *dev; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 586 | char *ptr = req->request.data, *err_msg = ""; |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 587 | char error[256]; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 588 | int err, start, end, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | ptr += strlen("remove"); |
| 591 | while(isspace(*ptr)) ptr++; |
| 592 | dev = mconsole_find_dev(ptr); |
| 593 | if(dev == NULL){ |
| 594 | mconsole_reply(req, "Bad remove option", 1, 0); |
| 595 | return; |
| 596 | } |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 597 | |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 598 | ptr = &ptr[strlen(dev->name)]; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 599 | |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 600 | err = 1; |
| 601 | n = (*dev->id)(&ptr, &start, &end); |
| 602 | if(n < 0){ |
| 603 | err_msg = "Couldn't parse device number"; |
| 604 | goto out; |
| 605 | } |
| 606 | else if((n < start) || (n > end)){ |
| 607 | sprintf(error, "Invalid device number - must be between " |
| 608 | "%d and %d", start, end); |
| 609 | err_msg = error; |
| 610 | goto out; |
| 611 | } |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 612 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 613 | err_msg = NULL; |
| 614 | err = (*dev->remove)(n, &err_msg); |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 615 | switch(err){ |
Jeff Dike | d40f6d7 | 2007-03-29 01:20:28 -0700 | [diff] [blame] | 616 | case 0: |
| 617 | err_msg = ""; |
| 618 | break; |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 619 | case -ENODEV: |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 620 | if(err_msg == NULL) |
| 621 | err_msg = "Device doesn't exist"; |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 622 | break; |
| 623 | case -EBUSY: |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 624 | if(err_msg == NULL) |
| 625 | err_msg = "Device is currently open"; |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 626 | break; |
| 627 | default: |
| 628 | break; |
| 629 | } |
| 630 | out: |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 631 | mconsole_reply(req, err_msg, err, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 634 | struct mconsole_output { |
| 635 | struct list_head list; |
| 636 | struct mc_request *req; |
| 637 | }; |
| 638 | |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 639 | static DEFINE_SPINLOCK(client_lock); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 640 | static LIST_HEAD(clients); |
| 641 | static char console_buf[MCONSOLE_MAX_DATA]; |
| 642 | static int console_index = 0; |
| 643 | |
| 644 | static void console_write(struct console *console, const char *string, |
| 645 | unsigned len) |
| 646 | { |
| 647 | struct list_head *ele; |
| 648 | int n; |
| 649 | |
| 650 | if(list_empty(&clients)) |
| 651 | return; |
| 652 | |
| 653 | while(1){ |
Paolo 'Blaisorblade' Giarrusso | 6dad2d3 | 2006-04-10 22:53:31 -0700 | [diff] [blame] | 654 | n = min((size_t) len, ARRAY_SIZE(console_buf) - console_index); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 655 | strncpy(&console_buf[console_index], string, n); |
| 656 | console_index += n; |
| 657 | string += n; |
| 658 | len -= n; |
| 659 | if(len == 0) |
| 660 | return; |
| 661 | |
| 662 | list_for_each(ele, &clients){ |
Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 663 | struct mconsole_output *entry; |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 664 | |
Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 665 | entry = list_entry(ele, struct mconsole_output, list); |
| 666 | mconsole_reply_len(entry->req, console_buf, |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 667 | console_index, 0, 1); |
| 668 | } |
| 669 | |
| 670 | console_index = 0; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | static struct console mc_console = { .name = "mc", |
| 675 | .write = console_write, |
Jeff Dike | a174b30 | 2006-01-11 12:17:28 -0800 | [diff] [blame] | 676 | .flags = CON_ENABLED, |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 677 | .index = -1 }; |
| 678 | |
| 679 | static int mc_add_console(void) |
| 680 | { |
| 681 | register_console(&mc_console); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | late_initcall(mc_add_console); |
| 686 | |
| 687 | static void with_console(struct mc_request *req, void (*proc)(void *), |
| 688 | void *arg) |
| 689 | { |
Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 690 | struct mconsole_output entry; |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 691 | unsigned long flags; |
| 692 | |
Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 693 | entry.req = req; |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 694 | spin_lock_irqsave(&client_lock, flags); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 695 | list_add(&entry.list, &clients); |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 696 | spin_unlock_irqrestore(&client_lock, flags); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 697 | |
| 698 | (*proc)(arg); |
| 699 | |
| 700 | mconsole_reply_len(req, console_buf, console_index, 0, 0); |
| 701 | console_index = 0; |
| 702 | |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 703 | spin_lock_irqsave(&client_lock, flags); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 704 | list_del(&entry.list); |
Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 705 | spin_unlock_irqrestore(&client_lock, flags); |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Jeff Dike | 4111b02 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 708 | #ifdef CONFIG_MAGIC_SYSRQ |
| 709 | static void sysrq_proc(void *arg) |
| 710 | { |
| 711 | char *op = arg; |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 712 | handle_sysrq(*op, NULL); |
Jeff Dike | 4111b02 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void mconsole_sysrq(struct mc_request *req) |
| 716 | { |
| 717 | char *ptr = req->request.data; |
| 718 | |
| 719 | ptr += strlen("sysrq"); |
| 720 | while(isspace(*ptr)) ptr++; |
| 721 | |
| 722 | /* With 'b', the system will shut down without a chance to reply, |
| 723 | * so in this case, we reply first. |
| 724 | */ |
| 725 | if(*ptr == 'b') |
| 726 | mconsole_reply(req, "", 0, 0); |
| 727 | |
| 728 | with_console(req, sysrq_proc, ptr); |
| 729 | } |
| 730 | #else |
| 731 | void mconsole_sysrq(struct mc_request *req) |
| 732 | { |
| 733 | mconsole_reply(req, "Sysrq not compiled in", 1, 0); |
| 734 | } |
| 735 | #endif |
| 736 | |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 737 | static void stack_proc(void *arg) |
| 738 | { |
| 739 | struct task_struct *from = current, *to = arg; |
| 740 | |
| 741 | to->thread.saved_task = from; |
| 742 | switch_to(from, to, from); |
| 743 | } |
| 744 | |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 745 | /* Mconsole stack trace |
| 746 | * Added by Allan Graves, Jeff Dike |
| 747 | * Dumps a stacks registers to the linux console. |
| 748 | * Usage stack <pid>. |
| 749 | */ |
Jeff Dike | 42fda66 | 2007-10-16 01:26:50 -0700 | [diff] [blame] | 750 | void mconsole_stack(struct mc_request *req) |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 751 | { |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 752 | char *ptr = req->request.data; |
| 753 | int pid_requested= -1; |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 754 | struct task_struct *from = NULL; |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 755 | struct task_struct *to = NULL; |
| 756 | |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 757 | /* Would be nice: |
| 758 | * 1) Send showregs output to mconsole. |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 759 | * 2) Add a way to stack dump all pids. |
| 760 | */ |
| 761 | |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 762 | ptr += strlen("stack"); |
| 763 | while(isspace(*ptr)) ptr++; |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 764 | |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 765 | /* Should really check for multiple pids or reject bad args here */ |
| 766 | /* What do the arguments in mconsole_reply mean? */ |
| 767 | if(sscanf(ptr, "%d", &pid_requested) == 0){ |
| 768 | mconsole_reply(req, "Please specify a pid", 1, 0); |
| 769 | return; |
| 770 | } |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 771 | |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 772 | from = current; |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 773 | |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 774 | to = find_task_by_pid(pid_requested); |
Jeff Dike | 3a331a5 | 2006-01-06 00:19:05 -0800 | [diff] [blame] | 775 | if((to == NULL) || (pid_requested == 0)) { |
| 776 | mconsole_reply(req, "Couldn't find that pid", 1, 0); |
| 777 | return; |
| 778 | } |
Jeff Dike | 6f517d3 | 2006-01-06 00:19:04 -0800 | [diff] [blame] | 779 | with_console(req, stack_proc, to); |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 780 | } |
Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | /* Changed by mconsole_setup, which is __setup, and called before SMP is |
| 783 | * active. |
| 784 | */ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 785 | static char *notify_socket = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 787 | static int __init mconsole_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | /* long to avoid size mismatch warnings from gcc */ |
| 790 | long sock; |
| 791 | int err; |
| 792 | char file[256]; |
| 793 | |
| 794 | if(umid_file_name("mconsole", file, sizeof(file))) return(-1); |
| 795 | snprintf(mconsole_socket_name, sizeof(file), "%s", file); |
| 796 | |
| 797 | sock = os_create_unix_socket(file, sizeof(file), 1); |
| 798 | if (sock < 0){ |
| 799 | printk("Failed to initialize management console\n"); |
| 800 | return(1); |
| 801 | } |
| 802 | |
| 803 | register_reboot_notifier(&reboot_notifier); |
| 804 | |
| 805 | err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt, |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 806 | IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | "mconsole", (void *)sock); |
| 808 | if (err){ |
| 809 | printk("Failed to get IRQ for management console\n"); |
| 810 | return(1); |
| 811 | } |
| 812 | |
| 813 | if(notify_socket != NULL){ |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 814 | notify_socket = kstrdup(notify_socket, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | if(notify_socket != NULL) |
| 816 | mconsole_notify(notify_socket, MCONSOLE_SOCKET, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 817 | mconsole_socket_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | strlen(mconsole_socket_name) + 1); |
| 819 | else printk(KERN_ERR "mconsole_setup failed to strdup " |
| 820 | "string\n"); |
| 821 | } |
| 822 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 823 | printk("mconsole (version %d) initialized on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | MCONSOLE_VERSION, mconsole_socket_name); |
| 825 | return(0); |
| 826 | } |
| 827 | |
| 828 | __initcall(mconsole_init); |
| 829 | |
| 830 | static int write_proc_mconsole(struct file *file, const char __user *buffer, |
| 831 | unsigned long count, void *data) |
| 832 | { |
| 833 | char *buf; |
| 834 | |
| 835 | buf = kmalloc(count + 1, GFP_KERNEL); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 836 | if(buf == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return(-ENOMEM); |
| 838 | |
| 839 | if(copy_from_user(buf, buffer, count)){ |
| 840 | count = -EFAULT; |
| 841 | goto out; |
| 842 | } |
| 843 | |
| 844 | buf[count] = '\0'; |
| 845 | |
| 846 | mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count); |
| 847 | out: |
| 848 | kfree(buf); |
| 849 | return(count); |
| 850 | } |
| 851 | |
| 852 | static int create_proc_mconsole(void) |
| 853 | { |
| 854 | struct proc_dir_entry *ent; |
| 855 | |
| 856 | if(notify_socket == NULL) return(0); |
| 857 | |
| 858 | ent = create_proc_entry("mconsole", S_IFREG | 0200, NULL); |
| 859 | if(ent == NULL){ |
Christophe Lucas | 30f417c | 2005-07-28 21:16:12 -0700 | [diff] [blame] | 860 | printk(KERN_INFO "create_proc_mconsole : create_proc_entry failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return(0); |
| 862 | } |
| 863 | |
| 864 | ent->read_proc = NULL; |
| 865 | ent->write_proc = write_proc_mconsole; |
| 866 | return(0); |
| 867 | } |
| 868 | |
| 869 | static DEFINE_SPINLOCK(notify_spinlock); |
| 870 | |
| 871 | void lock_notify(void) |
| 872 | { |
| 873 | spin_lock(¬ify_spinlock); |
| 874 | } |
| 875 | |
| 876 | void unlock_notify(void) |
| 877 | { |
| 878 | spin_unlock(¬ify_spinlock); |
| 879 | } |
| 880 | |
| 881 | __initcall(create_proc_mconsole); |
| 882 | |
| 883 | #define NOTIFY "=notify:" |
| 884 | |
| 885 | static int mconsole_setup(char *str) |
| 886 | { |
| 887 | if(!strncmp(str, NOTIFY, strlen(NOTIFY))){ |
| 888 | str += strlen(NOTIFY); |
| 889 | notify_socket = str; |
| 890 | } |
| 891 | else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str); |
| 892 | return(1); |
| 893 | } |
| 894 | |
| 895 | __setup("mconsole", mconsole_setup); |
| 896 | |
| 897 | __uml_help(mconsole_setup, |
| 898 | "mconsole=notify:<socket>\n" |
| 899 | " Requests that the mconsole driver send a message to the named Unix\n" |
| 900 | " socket containing the name of the mconsole socket. This also serves\n" |
| 901 | " to notify outside processes when UML has booted far enough to respond\n" |
| 902 | " to mconsole requests.\n\n" |
| 903 | ); |
| 904 | |
| 905 | static int notify_panic(struct notifier_block *self, unsigned long unused1, |
| 906 | void *ptr) |
| 907 | { |
| 908 | char *message = ptr; |
| 909 | |
| 910 | if(notify_socket == NULL) return(0); |
| 911 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 912 | mconsole_notify(notify_socket, MCONSOLE_PANIC, message, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | strlen(message) + 1); |
| 914 | return(0); |
| 915 | } |
| 916 | |
| 917 | static struct notifier_block panic_exit_notifier = { |
| 918 | .notifier_call = notify_panic, |
| 919 | .next = NULL, |
| 920 | .priority = 1 |
| 921 | }; |
| 922 | |
| 923 | static int add_notifier(void) |
| 924 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 925 | atomic_notifier_chain_register(&panic_notifier_list, |
| 926 | &panic_exit_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return(0); |
| 928 | } |
| 929 | |
| 930 | __initcall(add_notifier); |
| 931 | |
| 932 | char *mconsole_notify_socket(void) |
| 933 | { |
| 934 | return(notify_socket); |
| 935 | } |
| 936 | |
| 937 | EXPORT_SYMBOL(mconsole_notify_socket); |