mconsole_kern.c 20 KB

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