mconsole_kern.c 20 KB

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