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