mconsole_kern.c 20 KB

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