mconsole_kern.c 20 KB

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