mconsole_kern.c 19.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
3
 * Copyright (C) 2001 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
4 5 6
 * Licensed under the GPL
 */

7 8
#include <linux/console.h>
#include <linux/ctype.h>
9
#include <linux/string.h>
10 11 12 13 14 15 16 17 18 19
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/utsname.h>
20 21
#include <linux/socket.h>
#include <linux/un.h>
22 23 24
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>
25
#include <asm/switch_to.h>
26

J
Jeff Dike 已提交
27 28 29
#include "init.h"
#include "irq_kern.h"
#include "irq_user.h"
L
Linus Torvalds 已提交
30 31 32 33 34
#include "kern_util.h"
#include "mconsole.h"
#include "mconsole_kern.h"
#include "os.h"

J
Jeff Dike 已提交
35
static int do_unlink_socket(struct notifier_block *notifier,
L
Linus Torvalds 已提交
36 37
			    unsigned long what, void *data)
{
J
Jeff Dike 已提交
38
	return mconsole_unlink_socket();
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46
}


static struct notifier_block reboot_notifier = {
	.notifier_call		= do_unlink_socket,
	.priority		= 0,
};

J
Jeff Dike 已提交
47
/* Safe without explicit locking for now.  Tasklets provide their own
L
Linus Torvalds 已提交
48 49 50 51
 * locking, and the interrupt handler is safe because it can't interrupt
 * itself and it can only happen on CPU 0.
 */

52
static LIST_HEAD(mc_requests);
L
Linus Torvalds 已提交
53

54
static void mc_work_proc(struct work_struct *unused)
L
Linus Torvalds 已提交
55 56 57 58
{
	struct mconsole_entry *req;
	unsigned long flags;

J
Jeff Dike 已提交
59
	while (!list_empty(&mc_requests)) {
60
		local_irq_save(flags);
J
Jeff Dike 已提交
61
		req = list_entry(mc_requests.next, struct mconsole_entry, list);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68
		list_del(&req->list);
		local_irq_restore(flags);
		req->request.cmd->handler(&req->request);
		kfree(req);
	}
}

69
static DECLARE_WORK(mconsole_work, mc_work_proc);
L
Linus Torvalds 已提交
70

A
Al Viro 已提交
71
static irqreturn_t mconsole_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
72 73 74 75
{
	/* long to avoid size mismatch warnings from gcc */
	long fd;
	struct mconsole_entry *new;
A
Al Viro 已提交
76
	static struct mc_request req;	/* that's OK */
L
Linus Torvalds 已提交
77 78

	fd = (long) dev_id;
J
Jeff Dike 已提交
79 80
	while (mconsole_get_request(fd, &req)) {
		if (req.cmd->context == MCONSOLE_INTR)
L
Linus Torvalds 已提交
81 82
			(*req.cmd->handler)(&req);
		else {
J
Jeff Dike 已提交
83
			new = kmalloc(sizeof(*new), GFP_NOWAIT);
J
Jeff Dike 已提交
84
			if (new == NULL)
L
Linus Torvalds 已提交
85 86 87
				mconsole_reply(&req, "Out of memory", 1, 0);
			else {
				new->request = req;
A
Al Viro 已提交
88
				new->request.regs = get_irq_regs()->regs;
L
Linus Torvalds 已提交
89 90 91 92
				list_add(&new->list, &mc_requests);
			}
		}
	}
J
Jeff Dike 已提交
93
	if (!list_empty(&mc_requests))
L
Linus Torvalds 已提交
94 95
		schedule_work(&mconsole_work);
	reactivate_fd(fd, MCONSOLE_IRQ);
J
Jeff Dike 已提交
96
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
97 98 99 100 101 102
}

void mconsole_version(struct mc_request *req)
{
	char version[256];

103
	sprintf(version, "%s %s %s %s %s", utsname()->sysname,
J
Jeff Dike 已提交
104 105
		utsname()->nodename, utsname()->release, utsname()->version,
		utsname()->machine);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116
	mconsole_reply(req, version, 0, 0);
}

void mconsole_log(struct mc_request *req)
{
	int len;
	char *ptr = req->request.data;

	ptr += strlen("log ");

	len = req->len - (ptr - req->request.data);
J
Jeff Dike 已提交
117
	printk(KERN_WARNING "%.*s", len, ptr);
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127
	mconsole_reply(req, "", 0, 0);
}

/* This is a more convoluted version of mconsole_proc, which has some stability
 * problems; however, we need it fixed, because it is expected that UML users
 * mount HPPFS instead of procfs on /proc. And we want mconsole_proc to still
 * show the real procfs content, not the ones from hppfs.*/
#if 0
void mconsole_proc(struct mc_request *req)
{
A
Al Viro 已提交
128
	struct vfsmount *mnt = current->nsproxy->pid_ns->proc_mnt;
L
Linus Torvalds 已提交
129
	struct file *file;
A
Al Viro 已提交
130
	int n;
L
Linus Torvalds 已提交
131
	char *ptr = req->request.data, *buf;
A
Al Viro 已提交
132
	mm_segment_t old_fs = get_fs();
L
Linus Torvalds 已提交
133 134

	ptr += strlen("proc");
135
	ptr = skip_spaces(ptr);
L
Linus Torvalds 已提交
136

A
Al Viro 已提交
137
	file = file_open_root(mnt->mnt_root, mnt, ptr, O_RDONLY);
J
Jeff Dike 已提交
138
	if (IS_ERR(file)) {
L
Linus Torvalds 已提交
139
		mconsole_reply(req, "Failed to open file", 1, 0);
A
Al Viro 已提交
140
		goto out;
L
Linus Torvalds 已提交
141 142 143
	}

	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
J
Jeff Dike 已提交
144
	if (buf == NULL) {
L
Linus Torvalds 已提交
145 146 147 148
		mconsole_reply(req, "Failed to allocate buffer", 1, 0);
		goto out_fput;
	}

A
Al Viro 已提交
149
	if (file->f_op->read) {
L
Linus Torvalds 已提交
150
		do {
A
Al Viro 已提交
151 152 153 154 155
			loff_t pos;
			set_fs(KERNEL_DS);
			n = vfs_read(file, buf, PAGE_SIZE - 1, &pos);
			file_pos_write(file, pos);
			set_fs(old_fs);
J
Jeff Dike 已提交
156
			if (n >= 0) {
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164
				buf[n] = '\0';
				mconsole_reply(req, buf, 0, (n > 0));
			}
			else {
				mconsole_reply(req, "Read of file failed",
					       1, 0);
				goto out_free;
			}
J
Jeff Dike 已提交
165
		} while (n > 0);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	}
	else mconsole_reply(req, "", 0, 0);

 out_free:
	kfree(buf);
 out_fput:
	fput(file);
 out: ;
}
#endif

void mconsole_proc(struct mc_request *req)
{
	char path[64];
	char *buf;
	int len;
	int fd;
	int first_chunk = 1;
	char *ptr = req->request.data;

	ptr += strlen("proc");
187
	ptr = skip_spaces(ptr);
L
Linus Torvalds 已提交
188 189 190 191 192
	snprintf(path, sizeof(path), "/proc/%s", ptr);

	fd = sys_open(path, 0, 0);
	if (fd < 0) {
		mconsole_reply(req, "Failed to open file", 1, 0);
J
Jeff Dike 已提交
193
		printk(KERN_ERR "open %s: %d\n",path,fd);
L
Linus Torvalds 已提交
194 195 196 197
		goto out;
	}

	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
J
Jeff Dike 已提交
198
	if (buf == NULL) {
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208
		mconsole_reply(req, "Failed to allocate buffer", 1, 0);
		goto out_close;
	}

	for (;;) {
		len = sys_read(fd, buf, PAGE_SIZE-1);
		if (len < 0) {
			mconsole_reply(req, "Read of file failed", 1, 0);
			goto out_free;
		}
J
Jeff Dike 已提交
209
		/* Begin the file content on his own line. */
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		if (first_chunk) {
			mconsole_reply(req, "\n", 0, 1);
			first_chunk = 0;
		}
		if (len == PAGE_SIZE-1) {
			buf[len] = '\0';
			mconsole_reply(req, buf, 0, 1);
		} else {
			buf[len] = '\0';
			mconsole_reply(req, buf, 0, 0);
			break;
		}
	}

 out_free:
	kfree(buf);
 out_close:
	sys_close(fd);
 out:
	/* nothing */;
}

#define UML_MCONSOLE_HELPTEXT \
"Commands: \n\
    version - Get kernel version \n\
    help - Print this message \n\
    halt - Halt UML \n\
    reboot - Reboot UML \n\
    config <dev>=<config> - Add a new device to UML;  \n\
	same syntax as command line \n\
    config <dev> - Query the configuration of a device \n\
    remove <dev> - Remove a device from UML \n\
    sysrq <letter> - Performs the SysRq action controlled by the letter \n\
J
Jeff Dike 已提交
243
    cad - invoke the Ctrl-Alt-Del handler \n\
L
Linus Torvalds 已提交
244 245 246 247
    stop - pause the UML; it will do nothing until it receives a 'go' \n\
    go - continue the UML after a 'stop' \n\
    log <string> - make UML enter <string> into the kernel log\n\
    proc <file> - returns the contents of the UML's /proc/<file>\n\
248
    stack <pid> - returns the stack of the specified pid\n\
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
"

void mconsole_help(struct mc_request *req)
{
	mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0);
}

void mconsole_halt(struct mc_request *req)
{
	mconsole_reply(req, "", 0, 0);
	machine_halt();
}

void mconsole_reboot(struct mc_request *req)
{
	mconsole_reply(req, "", 0, 0);
	machine_restart(NULL);
}

void mconsole_cad(struct mc_request *req)
{
	mconsole_reply(req, "", 0, 0);
	ctrl_alt_del();
}

void mconsole_go(struct mc_request *req)
{
	mconsole_reply(req, "Not stopped", 1, 0);
}

void mconsole_stop(struct mc_request *req)
{
	deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
	os_set_fd_block(req->originating_fd, 1);
A
Al Viro 已提交
283
	mconsole_reply(req, "stopped", 0, 0);
284 285 286
	for (;;) {
		if (!mconsole_get_request(req->originating_fd, req))
			continue;
A
Al Viro 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
		if (req->cmd->handler == mconsole_go)
			break;
		if (req->cmd->handler == mconsole_stop) {
			mconsole_reply(req, "Already stopped", 1, 0);
			continue;
		}
		if (req->cmd->handler == mconsole_sysrq) {
			struct pt_regs *old_regs;
			old_regs = set_irq_regs((struct pt_regs *)&req->regs);
			mconsole_sysrq(req);
			set_irq_regs(old_regs);
			continue;
		}
L
Linus Torvalds 已提交
300 301 302 303 304 305 306
		(*req->cmd->handler)(req);
	}
	os_set_fd_block(req->originating_fd, 0);
	reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
	mconsole_reply(req, "", 0, 0);
}

J
Jeff Dike 已提交
307
static DEFINE_SPINLOCK(mc_devices_lock);
308
static LIST_HEAD(mconsole_devices);
L
Linus Torvalds 已提交
309 310 311

void mconsole_register_dev(struct mc_device *new)
{
J
Jeff Dike 已提交
312 313
	spin_lock(&mc_devices_lock);
	BUG_ON(!list_empty(&new->list));
L
Linus Torvalds 已提交
314
	list_add(&new->list, &mconsole_devices);
J
Jeff Dike 已提交
315
	spin_unlock(&mc_devices_lock);
L
Linus Torvalds 已提交
316 317 318 319 320 321 322
}

static struct mc_device *mconsole_find_dev(char *name)
{
	struct list_head *ele;
	struct mc_device *dev;

J
Jeff Dike 已提交
323
	list_for_each(ele, &mconsole_devices) {
L
Linus Torvalds 已提交
324
		dev = list_entry(ele, struct mc_device, list);
J
Jeff Dike 已提交
325 326
		if (!strncmp(name, dev->name, strlen(dev->name)))
			return dev;
L
Linus Torvalds 已提交
327
	}
J
Jeff Dike 已提交
328
	return NULL;
L
Linus Torvalds 已提交
329 330
}

J
Jeff Dike 已提交
331 332 333 334 335 336 337 338
#define UNPLUGGED_PER_PAGE \
	((PAGE_SIZE - sizeof(struct list_head)) / sizeof(unsigned long))

struct unplugged_pages {
	struct list_head list;
	void *pages[UNPLUGGED_PER_PAGE];
};

D
Daniel Walker 已提交
339
static DEFINE_MUTEX(plug_mem_mutex);
J
Jeff Dike 已提交
340
static unsigned long long unplugged_pages_count = 0;
341
static LIST_HEAD(unplugged_pages);
J
Jeff Dike 已提交
342 343
static int unplug_index = UNPLUGGED_PER_PAGE;

344
static int mem_config(char *str, char **error_out)
J
Jeff Dike 已提交
345 346 347 348 349
{
	unsigned long long diff;
	int err = -EINVAL, i, add;
	char *ret;

J
Jeff Dike 已提交
350
	if (str[0] != '=') {
351
		*error_out = "Expected '=' after 'mem'";
J
Jeff Dike 已提交
352
		goto out;
353
	}
J
Jeff Dike 已提交
354 355

	str++;
J
Jeff Dike 已提交
356
	if (str[0] == '-')
J
Jeff Dike 已提交
357
		add = 0;
J
Jeff Dike 已提交
358
	else if (str[0] == '+') {
J
Jeff Dike 已提交
359 360
		add = 1;
	}
361 362 363 364
	else {
		*error_out = "Expected increment to start with '-' or '+'";
		goto out;
	}
J
Jeff Dike 已提交
365 366 367

	str++;
	diff = memparse(str, &ret);
J
Jeff Dike 已提交
368
	if (*ret != '\0') {
369
		*error_out = "Failed to parse memory increment";
J
Jeff Dike 已提交
370
		goto out;
371
	}
J
Jeff Dike 已提交
372 373 374

	diff /= PAGE_SIZE;

D
Daniel Walker 已提交
375
	mutex_lock(&plug_mem_mutex);
J
Jeff Dike 已提交
376
	for (i = 0; i < diff; i++) {
J
Jeff Dike 已提交
377 378 379
		struct unplugged_pages *unplugged;
		void *addr;

J
Jeff Dike 已提交
380 381
		if (add) {
			if (list_empty(&unplugged_pages))
J
Jeff Dike 已提交
382 383 384 385
				break;

			unplugged = list_entry(unplugged_pages.next,
					       struct unplugged_pages, list);
J
Jeff Dike 已提交
386
			if (unplug_index > 0)
J
Jeff Dike 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
				addr = unplugged->pages[--unplug_index];
			else {
				list_del(&unplugged->list);
				addr = unplugged;
				unplug_index = UNPLUGGED_PER_PAGE;
			}

			free_page((unsigned long) addr);
			unplugged_pages_count--;
		}
		else {
			struct page *page;

			page = alloc_page(GFP_ATOMIC);
J
Jeff Dike 已提交
401
			if (page == NULL)
J
Jeff Dike 已提交
402 403 404
				break;

			unplugged = page_address(page);
J
Jeff Dike 已提交
405
			if (unplug_index == UNPLUGGED_PER_PAGE) {
J
Jeff Dike 已提交
406 407 408 409 410 411 412 413 414 415 416
				list_add(&unplugged->list, &unplugged_pages);
				unplug_index = 0;
			}
			else {
				struct list_head *entry = unplugged_pages.next;
				addr = unplugged;

				unplugged = list_entry(entry,
						       struct unplugged_pages,
						       list);
				err = os_drop_memory(addr, PAGE_SIZE);
J
Jeff Dike 已提交
417 418 419
				if (err) {
					printk(KERN_ERR "Failed to release "
					       "memory - errno = %d\n", err);
420
					*error_out = "Failed to release memory";
J
Jeff Dike 已提交
421
					goto out_unlock;
422 423
				}
				unplugged->pages[unplug_index++] = addr;
J
Jeff Dike 已提交
424 425 426 427 428 429 430
			}

			unplugged_pages_count++;
		}
	}

	err = 0;
J
Jeff Dike 已提交
431
out_unlock:
D
Daniel Walker 已提交
432
	mutex_unlock(&plug_mem_mutex);
J
Jeff Dike 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
out:
	return err;
}

static int mem_get_config(char *name, char *str, int size, char **error_out)
{
	char buf[sizeof("18446744073709551615")];
	int len = 0;

	sprintf(buf, "%ld", uml_physmem);
	CONFIG_CHUNK(str, size, len, buf, 1);

	return len;
}

static int mem_id(char **str, int *start_out, int *end_out)
{
	*start_out = 0;
	*end_out = 0;

	return 0;
}

456
static int mem_remove(int n, char **error_out)
J
Jeff Dike 已提交
457
{
458
	*error_out = "Memory doesn't support the remove operation";
J
Jeff Dike 已提交
459 460 461 462
	return -EBUSY;
}

static struct mc_device mem_mc = {
J
Jeff Dike 已提交
463
	.list		= LIST_HEAD_INIT(mem_mc.list),
J
Jeff Dike 已提交
464 465 466 467 468 469 470
	.name		= "mem",
	.config		= mem_config,
	.get_config	= mem_get_config,
	.id		= mem_id,
	.remove		= mem_remove,
};

J
Jeff Dike 已提交
471
static int __init mem_mc_init(void)
J
Jeff Dike 已提交
472
{
J
Jeff Dike 已提交
473
	if (can_drop_memory())
J
Jeff Dike 已提交
474
		mconsole_register_dev(&mem_mc);
J
Jeff Dike 已提交
475 476
	else printk(KERN_ERR "Can't release memory to the host - memory "
		    "hotplug won't be supported\n");
J
Jeff Dike 已提交
477 478 479 480 481
	return 0;
}

__initcall(mem_mc_init);

L
Linus Torvalds 已提交
482 483
#define CONFIG_BUF_SIZE 64

J
Jeff Dike 已提交
484
static void mconsole_get_config(int (*get_config)(char *, char *, int,
L
Linus Torvalds 已提交
485 486 487 488 489 490
						  char **),
				struct mc_request *req, char *name)
{
	char default_buf[CONFIG_BUF_SIZE], *error, *buf;
	int n, size;

J
Jeff Dike 已提交
491
	if (get_config == NULL) {
L
Linus Torvalds 已提交
492 493 494 495 496
		mconsole_reply(req, "No get_config routine defined", 1, 0);
		return;
	}

	error = NULL;
497
	size = ARRAY_SIZE(default_buf);
L
Linus Torvalds 已提交
498 499
	buf = default_buf;

J
Jeff Dike 已提交
500
	while (1) {
L
Linus Torvalds 已提交
501
		n = (*get_config)(name, buf, size, &error);
J
Jeff Dike 已提交
502
		if (error != NULL) {
L
Linus Torvalds 已提交
503 504 505 506
			mconsole_reply(req, error, 1, 0);
			goto out;
		}

J
Jeff Dike 已提交
507
		if (n <= size) {
L
Linus Torvalds 已提交
508 509 510 511
			mconsole_reply(req, buf, 0, 0);
			goto out;
		}

J
Jeff Dike 已提交
512
		if (buf != default_buf)
L
Linus Torvalds 已提交
513 514 515 516
			kfree(buf);

		size = n;
		buf = kmalloc(size, GFP_KERNEL);
J
Jeff Dike 已提交
517
		if (buf == NULL) {
L
Linus Torvalds 已提交
518 519 520 521 522
			mconsole_reply(req, "Failed to allocate buffer", 1, 0);
			return;
		}
	}
 out:
J
Jeff Dike 已提交
523
	if (buf != default_buf)
L
Linus Torvalds 已提交
524 525 526 527 528 529
		kfree(buf);
}

void mconsole_config(struct mc_request *req)
{
	struct mc_device *dev;
530
	char *ptr = req->request.data, *name, *error_string = "";
L
Linus Torvalds 已提交
531 532 533
	int err;

	ptr += strlen("config");
534
	ptr = skip_spaces(ptr);
L
Linus Torvalds 已提交
535
	dev = mconsole_find_dev(ptr);
J
Jeff Dike 已提交
536
	if (dev == NULL) {
L
Linus Torvalds 已提交
537 538 539 540 541 542
		mconsole_reply(req, "Bad configuration option", 1, 0);
		return;
	}

	name = &ptr[strlen(dev->name)];
	ptr = name;
J
Jeff Dike 已提交
543
	while ((*ptr != '=') && (*ptr != '\0'))
L
Linus Torvalds 已提交
544 545
		ptr++;

J
Jeff Dike 已提交
546
	if (*ptr == '=') {
547 548
		err = (*dev->config)(name, &error_string);
		mconsole_reply(req, error_string, err, 0);
L
Linus Torvalds 已提交
549 550 551 552 553 554
	}
	else mconsole_get_config(dev->get_config, req, name);
}

void mconsole_remove(struct mc_request *req)
{
J
Jeff Dike 已提交
555
	struct mc_device *dev;
J
Jeff Dike 已提交
556
	char *ptr = req->request.data, *err_msg = "";
557
	char error[256];
J
Jeff Dike 已提交
558
	int err, start, end, n;
L
Linus Torvalds 已提交
559 560

	ptr += strlen("remove");
561
	ptr = skip_spaces(ptr);
L
Linus Torvalds 已提交
562
	dev = mconsole_find_dev(ptr);
J
Jeff Dike 已提交
563
	if (dev == NULL) {
L
Linus Torvalds 已提交
564 565 566
		mconsole_reply(req, "Bad remove option", 1, 0);
		return;
	}
J
Jeff Dike 已提交
567

568 569 570 571
	ptr = &ptr[strlen(dev->name)];

	err = 1;
	n = (*dev->id)(&ptr, &start, &end);
J
Jeff Dike 已提交
572
	if (n < 0) {
573 574 575
		err_msg = "Couldn't parse device number";
		goto out;
	}
J
Jeff Dike 已提交
576
	else if ((n < start) || (n > end)) {
577 578 579 580 581
		sprintf(error, "Invalid device number - must be between "
			"%d and %d", start, end);
		err_msg = error;
		goto out;
	}
J
Jeff Dike 已提交
582

583 584
	err_msg = NULL;
	err = (*dev->remove)(n, &err_msg);
J
Jeff Dike 已提交
585
	switch(err) {
J
Jeff Dike 已提交
586 587 588
	case 0:
		err_msg = "";
		break;
589
	case -ENODEV:
J
Jeff Dike 已提交
590
		if (err_msg == NULL)
591
			err_msg = "Device doesn't exist";
592 593
		break;
	case -EBUSY:
J
Jeff Dike 已提交
594
		if (err_msg == NULL)
595
			err_msg = "Device is currently open";
596 597 598 599 600
		break;
	default:
		break;
	}
out:
J
Jeff Dike 已提交
601
	mconsole_reply(req, err_msg, err, 0);
L
Linus Torvalds 已提交
602 603
}

604 605 606 607 608
struct mconsole_output {
	struct list_head list;
	struct mc_request *req;
};

J
Jeff Dike 已提交
609
static DEFINE_SPINLOCK(client_lock);
610 611 612 613
static LIST_HEAD(clients);
static char console_buf[MCONSOLE_MAX_DATA];

static void console_write(struct console *console, const char *string,
J
Jeff Dike 已提交
614
			  unsigned int len)
615 616 617 618
{
	struct list_head *ele;
	int n;

J
Jeff Dike 已提交
619
	if (list_empty(&clients))
620 621
		return;

J
Jeff Dike 已提交
622 623 624
	while (len > 0) {
		n = min((size_t) len, ARRAY_SIZE(console_buf));
		strncpy(console_buf, string, n);
625 626 627
		string += n;
		len -= n;

J
Jeff Dike 已提交
628
		list_for_each(ele, &clients) {
629
			struct mconsole_output *entry;
630

631
			entry = list_entry(ele, struct mconsole_output, list);
J
Jeff Dike 已提交
632
			mconsole_reply_len(entry->req, console_buf, n, 0, 1);
633 634 635 636 637 638
		}
	}
}

static struct console mc_console = { .name	= "mc",
				     .write	= console_write,
639
				     .flags	= CON_ENABLED,
640 641 642 643 644 645 646 647 648 649 650 651 652
				     .index	= -1 };

static int mc_add_console(void)
{
	register_console(&mc_console);
	return 0;
}

late_initcall(mc_add_console);

static void with_console(struct mc_request *req, void (*proc)(void *),
			 void *arg)
{
653
	struct mconsole_output entry;
654 655
	unsigned long flags;

656
	entry.req = req;
J
Jeff Dike 已提交
657
	spin_lock_irqsave(&client_lock, flags);
658
	list_add(&entry.list, &clients);
J
Jeff Dike 已提交
659
	spin_unlock_irqrestore(&client_lock, flags);
660 661 662

	(*proc)(arg);

J
Jeff Dike 已提交
663
	mconsole_reply_len(req, "", 0, 0, 0);
664

J
Jeff Dike 已提交
665
	spin_lock_irqsave(&client_lock, flags);
666
	list_del(&entry.list);
J
Jeff Dike 已提交
667
	spin_unlock_irqrestore(&client_lock, flags);
668 669
}

670
#ifdef CONFIG_MAGIC_SYSRQ
J
Jeff Dike 已提交
671 672 673

#include <linux/sysrq.h>

674 675 676
static void sysrq_proc(void *arg)
{
	char *op = arg;
677
	handle_sysrq(*op);
678 679 680 681 682 683 684
}

void mconsole_sysrq(struct mc_request *req)
{
	char *ptr = req->request.data;

	ptr += strlen("sysrq");
685
	ptr = skip_spaces(ptr);
686

J
Jeff Dike 已提交
687 688
	/*
	 * With 'b', the system will shut down without a chance to reply,
689 690
	 * so in this case, we reply first.
	 */
J
Jeff Dike 已提交
691
	if (*ptr == 'b')
692 693 694 695 696 697 698 699 700 701 702
		mconsole_reply(req, "", 0, 0);

	with_console(req, sysrq_proc, ptr);
}
#else
void mconsole_sysrq(struct mc_request *req)
{
	mconsole_reply(req, "Sysrq not compiled in", 1, 0);
}
#endif

703 704 705 706 707 708 709 710
static void stack_proc(void *arg)
{
	struct task_struct *from = current, *to = arg;

	to->thread.saved_task = from;
	switch_to(from, to, from);
}

J
Jeff Dike 已提交
711 712
/*
 * Mconsole stack trace
713 714 715 716
 *  Added by Allan Graves, Jeff Dike
 *  Dumps a stacks registers to the linux console.
 *  Usage stack <pid>.
 */
J
Jeff Dike 已提交
717
void mconsole_stack(struct mc_request *req)
718
{
719 720
	char *ptr = req->request.data;
	int pid_requested= -1;
721 722
	struct task_struct *to = NULL;

J
Jeff Dike 已提交
723 724
	/*
	 * Would be nice:
725
	 * 1) Send showregs output to mconsole.
726 727 728
	 * 2) Add a way to stack dump all pids.
	 */

729
	ptr += strlen("stack");
730
	ptr = skip_spaces(ptr);
731

J
Jeff Dike 已提交
732 733 734
	/*
	 * Should really check for multiple pids or reject bad args here
	 */
735
	/* What do the arguments in mconsole_reply mean? */
J
Jeff Dike 已提交
736
	if (sscanf(ptr, "%d", &pid_requested) == 0) {
737 738 739
		mconsole_reply(req, "Please specify a pid", 1, 0);
		return;
	}
740

741
	to = find_task_by_pid_ns(pid_requested, &init_pid_ns);
J
Jeff Dike 已提交
742
	if ((to == NULL) || (pid_requested == 0)) {
743 744 745
		mconsole_reply(req, "Couldn't find that pid", 1, 0);
		return;
	}
746
	with_console(req, stack_proc, to);
747 748
}

J
Jeff Dike 已提交
749 750
/*
 * Changed by mconsole_setup, which is __setup, and called before SMP is
L
Linus Torvalds 已提交
751 752
 * active.
 */
J
Jeff Dike 已提交
753
static char *notify_socket = NULL;
L
Linus Torvalds 已提交
754

J
Jeff Dike 已提交
755
static int __init mconsole_init(void)
L
Linus Torvalds 已提交
756 757 758 759
{
	/* long to avoid size mismatch warnings from gcc */
	long sock;
	int err;
760
	char file[UNIX_PATH_MAX];
L
Linus Torvalds 已提交
761

J
Jeff Dike 已提交
762 763
	if (umid_file_name("mconsole", file, sizeof(file)))
		return -1;
L
Linus Torvalds 已提交
764 765 766
	snprintf(mconsole_socket_name, sizeof(file), "%s", file);

	sock = os_create_unix_socket(file, sizeof(file), 1);
J
Jeff Dike 已提交
767 768 769
	if (sock < 0) {
		printk(KERN_ERR "Failed to initialize management console\n");
		return 1;
L
Linus Torvalds 已提交
770
	}
J
Jeff Dike 已提交
771 772
	if (os_set_fd_block(sock, 0))
		goto out;
L
Linus Torvalds 已提交
773 774 775 776

	register_reboot_notifier(&reboot_notifier);

	err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
Y
Yong Zhang 已提交
777
			     IRQF_SHARED | IRQF_SAMPLE_RANDOM,
L
Linus Torvalds 已提交
778
			     "mconsole", (void *)sock);
J
Jeff Dike 已提交
779 780
	if (err) {
		printk(KERN_ERR "Failed to get IRQ for management console\n");
J
Jeff Dike 已提交
781
		goto out;
L
Linus Torvalds 已提交
782 783
	}

J
Jeff Dike 已提交
784
	if (notify_socket != NULL) {
J
Jeff Dike 已提交
785
		notify_socket = kstrdup(notify_socket, GFP_KERNEL);
J
Jeff Dike 已提交
786
		if (notify_socket != NULL)
L
Linus Torvalds 已提交
787
			mconsole_notify(notify_socket, MCONSOLE_SOCKET,
J
Jeff Dike 已提交
788
					mconsole_socket_name,
L
Linus Torvalds 已提交
789 790 791 792 793
					strlen(mconsole_socket_name) + 1);
		else printk(KERN_ERR "mconsole_setup failed to strdup "
			    "string\n");
	}

J
Jeff Dike 已提交
794
	printk(KERN_INFO "mconsole (version %d) initialized on %s\n",
L
Linus Torvalds 已提交
795
	       MCONSOLE_VERSION, mconsole_socket_name);
J
Jeff Dike 已提交
796
	return 0;
J
Jeff Dike 已提交
797 798 799 800

 out:
	os_close_file(sock);
	return 1;
L
Linus Torvalds 已提交
801 802 803 804
}

__initcall(mconsole_init);

805 806
static ssize_t mconsole_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
807 808 809 810
{
	char *buf;

	buf = kmalloc(count + 1, GFP_KERNEL);
J
Jeff Dike 已提交
811 812
	if (buf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
813

J
Jeff Dike 已提交
814
	if (copy_from_user(buf, buffer, count)) {
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822 823
		count = -EFAULT;
		goto out;
	}

	buf[count] = '\0';

	mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
 out:
	kfree(buf);
J
Jeff Dike 已提交
824
	return count;
L
Linus Torvalds 已提交
825 826
}

827 828 829
static const struct file_operations mconsole_proc_fops = {
	.owner		= THIS_MODULE,
	.write		= mconsole_proc_write,
830
	.llseek		= noop_llseek,
831 832
};

L
Linus Torvalds 已提交
833 834 835 836
static int create_proc_mconsole(void)
{
	struct proc_dir_entry *ent;

J
Jeff Dike 已提交
837 838
	if (notify_socket == NULL)
		return 0;
L
Linus Torvalds 已提交
839

840
	ent = proc_create("mconsole", 0200, NULL, &mconsole_proc_fops);
J
Jeff Dike 已提交
841 842 843 844
	if (ent == NULL) {
		printk(KERN_INFO "create_proc_mconsole : create_proc_entry "
		       "failed\n");
		return 0;
L
Linus Torvalds 已提交
845
	}
J
Jeff Dike 已提交
846
	return 0;
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
}

static DEFINE_SPINLOCK(notify_spinlock);

void lock_notify(void)
{
	spin_lock(&notify_spinlock);
}

void unlock_notify(void)
{
	spin_unlock(&notify_spinlock);
}

__initcall(create_proc_mconsole);

J
Jeff Dike 已提交
863
#define NOTIFY "notify:"
L
Linus Torvalds 已提交
864 865 866

static int mconsole_setup(char *str)
{
J
Jeff Dike 已提交
867
	if (!strncmp(str, NOTIFY, strlen(NOTIFY))) {
L
Linus Torvalds 已提交
868 869 870 871
		str += strlen(NOTIFY);
		notify_socket = str;
	}
	else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
J
Jeff Dike 已提交
872
	return 1;
L
Linus Torvalds 已提交
873 874
}

J
Jeff Dike 已提交
875
__setup("mconsole=", mconsole_setup);
L
Linus Torvalds 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889

__uml_help(mconsole_setup,
"mconsole=notify:<socket>\n"
"    Requests that the mconsole driver send a message to the named Unix\n"
"    socket containing the name of the mconsole socket.  This also serves\n"
"    to notify outside processes when UML has booted far enough to respond\n"
"    to mconsole requests.\n\n"
);

static int notify_panic(struct notifier_block *self, unsigned long unused1,
			void *ptr)
{
	char *message = ptr;

J
Jeff Dike 已提交
890 891
	if (notify_socket == NULL)
		return 0;
L
Linus Torvalds 已提交
892

J
Jeff Dike 已提交
893
	mconsole_notify(notify_socket, MCONSOLE_PANIC, message,
L
Linus Torvalds 已提交
894
			strlen(message) + 1);
J
Jeff Dike 已提交
895
	return 0;
L
Linus Torvalds 已提交
896 897 898 899 900 901 902 903 904 905
}

static struct notifier_block panic_exit_notifier = {
	.notifier_call 		= notify_panic,
	.next 			= NULL,
	.priority 		= 1
};

static int add_notifier(void)
{
906 907
	atomic_notifier_chain_register(&panic_notifier_list,
			&panic_exit_notifier);
J
Jeff Dike 已提交
908
	return 0;
L
Linus Torvalds 已提交
909 910 911 912 913 914
}

__initcall(add_notifier);

char *mconsole_notify_socket(void)
{
J
Jeff Dike 已提交
915
	return notify_socket;
L
Linus Torvalds 已提交
916 917 918
}

EXPORT_SYMBOL(mconsole_notify_socket);