mconsole_kern.c 21 KB

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