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