mconsole_kern.c 21 KB

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