mconsole_kern.c 19 KB

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