mconsole_kern.c 19 KB

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