mconsole_kern.c 20 KB

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