debug_sh.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Kernel Debug Shell
  7. */
  8. #include <kernel/system.h>
  9. #include <kernel/fs.h>
  10. #include <kernel/printf.h>
  11. #include <kernel/logging.h>
  12. #include <kernel/process.h>
  13. #include <kernel/version.h>
  14. #include <kernel/tokenize.h>
  15. #include <kernel/pci.h>
  16. #include <kernel/pipe.h>
  17. #include <kernel/elf.h>
  18. #include <kernel/module.h>
  19. #include <kernel/args.h>
  20. #include <kernel/mod/shell.h>
  21. #include <toaru/list.h>
  22. #include <toaru/hashmap.h>
  23. #include <sys/termios.h>
  24. /*
  25. * This is basically the same as a userspace buffered/unbuffered
  26. * termio call. These are the same sorts of things I would use in
  27. * a text editor in userspace, but with the internal kernel calls
  28. * rather than system calls.
  29. */
  30. static struct termios old;
  31. void tty_set_unbuffered(fs_node_t * dev) {
  32. ioctl_fs(dev, TCGETS, &old);
  33. struct termios new = old;
  34. new.c_lflag &= (~ICANON & ~ECHO);
  35. ioctl_fs(dev, TCSETSF, &new);
  36. }
  37. void tty_set_buffered(fs_node_t * dev) {
  38. ioctl_fs(dev, TCSETSF, &old);
  39. }
  40. void tty_set_vintr(fs_node_t * dev, char vintr) {
  41. struct termios tmp;
  42. ioctl_fs(dev, TCGETS, &tmp);
  43. tmp.c_cc[VINTR] = vintr;
  44. ioctl_fs(dev, TCSETSF, &tmp);
  45. }
  46. /*
  47. * Quick readline implementation.
  48. *
  49. * Most of these TODOs are things I've done already in older code:
  50. * TODO tabcompletion would be nice
  51. * TODO history is also nice
  52. */
  53. int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
  54. int read = 0;
  55. tty_set_unbuffered(dev);
  56. while (read < max) {
  57. uint8_t buf[1];
  58. int r = read_fs(dev, 0, 1, (unsigned char *)buf);
  59. if (!r) {
  60. debug_print(WARNING, "Read nothing?");
  61. continue;
  62. }
  63. linebuf[read] = buf[0];
  64. if (buf[0] == '\n') {
  65. fprintf(dev, "\n");
  66. linebuf[read] = 0;
  67. break;
  68. } else if (buf[0] == 0x08) {
  69. if (read > 0) {
  70. fprintf(dev, "\010 \010");
  71. read--;
  72. linebuf[read] = 0;
  73. }
  74. } else if (buf[0] < ' ') {
  75. switch (buf[0]) {
  76. case 0x04:
  77. if (read == 0) {
  78. fprintf(dev, "exit\n");
  79. sprintf(linebuf, "exit");
  80. return strlen(linebuf);
  81. }
  82. break;
  83. case 0x0C: /* ^L */
  84. /* Should reset display here */
  85. break;
  86. default:
  87. /* do nothing */
  88. break;
  89. }
  90. } else {
  91. fprintf(dev, "%c", buf[0]);
  92. read += r;
  93. }
  94. }
  95. tty_set_buffered(dev);
  96. return read;
  97. }
  98. /*
  99. * Tasklet for running a userspace application.
  100. */
  101. static void debug_shell_run_sh(void * data, char * name) {
  102. char * argv[] = {
  103. data,
  104. NULL
  105. };
  106. int argc = 0;
  107. while (argv[argc]) {
  108. argc++;
  109. }
  110. system(argv[0], argc, argv); /* Run shell */
  111. task_exit(42);
  112. }
  113. static hashmap_t * shell_commands_map = NULL;
  114. /*
  115. * Shell commands
  116. */
  117. static int shell_create_userspace_shell(fs_node_t * tty, int argc, char * argv[]) {
  118. int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/sh");
  119. fprintf(tty, "Shell started with pid = %d\n", pid);
  120. int status;
  121. waitpid(pid,&status,0);
  122. return status;
  123. }
  124. static int shell_replace_login(fs_node_t * tty, int argc, char * argv[]) {
  125. /* We need to fork to get a clean task space */
  126. create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/login");
  127. /* Then exit the shell process */
  128. task_exit(0);
  129. /* unreachable */
  130. return 0;
  131. }
  132. static int shell_echo(fs_node_t * tty, int argc, char * argv[]) {
  133. for (int i = 1; i < argc; ++i) {
  134. fprintf(tty, "%s ", argv[i]);
  135. }
  136. fprintf(tty, "\n");
  137. return 0;
  138. }
  139. static int dumb_strcmp(void * a, void *b) {
  140. return strcmp(a, b);
  141. }
  142. static void dumb_sort(void ** list, size_t length, int (*compare)(void*,void*)) {
  143. for (unsigned int i = 0; i < length-1; ++i) {
  144. for (unsigned int j = 0; j < length-1; ++j) {
  145. if (compare(list[j], list[j+1]) > 0) {
  146. void * t = list[j+1];
  147. list[j+1] = list[j];
  148. list[j] = t;
  149. }
  150. }
  151. }
  152. }
  153. static void print_spaces(fs_node_t * tty, int num_spaces) {
  154. for (int i = 0; i < num_spaces; ++i) {
  155. fprintf(tty, " ");
  156. }
  157. }
  158. static int shell_help(fs_node_t * tty, int argc, char * argv[]) {
  159. list_t * hash_keys = hashmap_keys(shell_commands_map);
  160. char ** keys = malloc(sizeof(char *) * hash_keys->length);
  161. unsigned int i = 0;
  162. unsigned int max_width = 0;
  163. foreach(_key, hash_keys) {
  164. char * key = (char *)_key->value;
  165. keys[i] = key;
  166. i++;
  167. if (strlen(key) > max_width) {
  168. max_width = strlen(key);
  169. }
  170. }
  171. dumb_sort((void **)keys, hash_keys->length, &dumb_strcmp);
  172. for (i = 0; i < hash_keys->length; ++i) {
  173. struct shell_command * c = hashmap_get(shell_commands_map, keys[i]);
  174. fprintf(tty, "\033[1;32m%s\033[0m ", c->name);
  175. print_spaces(tty, max_width- strlen(c->name));
  176. fprintf(tty, "- %s\n", c->description);
  177. }
  178. free(keys);
  179. list_free(hash_keys);
  180. free(hash_keys);
  181. return 0;
  182. }
  183. static int shell_cd(fs_node_t * tty, int argc, char * argv[]) {
  184. if (argc < 2) {
  185. return 1;
  186. }
  187. char * newdir = argv[1];
  188. char * path = canonicalize_path(current_process->wd_name, newdir);
  189. fs_node_t * chd = kopen(path, 0);
  190. if (chd) {
  191. if ((chd->flags & FS_DIRECTORY) == 0) {
  192. return 1;
  193. }
  194. close_fs(chd);
  195. free(current_process->wd_name);
  196. current_process->wd_name = malloc(strlen(path) + 1);
  197. memcpy(current_process->wd_name, path, strlen(path) + 1);
  198. return 0;
  199. } else {
  200. return 1;
  201. }
  202. }
  203. static int shell_ls(fs_node_t * tty, int argc, char * argv[]) {
  204. fs_node_t * wd;
  205. if (argc < 2) {
  206. wd = kopen(current_process->wd_name, 0);
  207. } else {
  208. wd = kopen(argv[1], 0);
  209. }
  210. uint32_t index = 0;
  211. struct dirent * kentry = readdir_fs(wd, index);
  212. while (kentry) {
  213. fprintf(tty, "%s\n", kentry->name);
  214. free(kentry);
  215. index++;
  216. kentry = readdir_fs(wd, index);
  217. }
  218. close_fs(wd);
  219. return 0;
  220. }
  221. static int shell_cat(fs_node_t * tty, int argc, char * argv[]) {
  222. if (argc < 2) {
  223. fprintf(tty, "Usage: cat <file>\n");
  224. return 1;
  225. }
  226. fs_node_t * node = kopen(argv[1], 0);
  227. if (!node) {
  228. fprintf(tty, "Could not open %s.\n", argv[1]);
  229. return 1;
  230. }
  231. #define CHUNK_SIZE 4096
  232. uint8_t * buf = malloc(CHUNK_SIZE);
  233. memset(buf, 0, CHUNK_SIZE);
  234. size_t offset = 0;
  235. while (1) {
  236. size_t r = read_fs(node, offset, CHUNK_SIZE, buf);
  237. if (!r) break;
  238. write_fs(tty, 0, r, buf);
  239. offset += r;
  240. }
  241. close_fs(node);
  242. return 0;
  243. }
  244. static int shell_log(fs_node_t * tty, int argc, char * argv[]) {
  245. if (argc < 2) {
  246. fprintf(tty, "Log level is currently %d.\n", debug_level);
  247. fprintf(tty, "Serial logging is %s.\n", !!debug_file ? "enabled" : "disabled");
  248. fprintf(tty, "Usage: log [on|off] [<level>]\n");
  249. } else {
  250. if (!strcmp(argv[1], "direct")) {
  251. debug_file = kopen("/dev/ttyS0", 0);
  252. if (argc > 2) {
  253. debug_level = atoi(argv[2]);
  254. }
  255. } else if (!strcmp(argv[1], "on")) {
  256. debug_file = tty;
  257. if (argc > 2) {
  258. debug_level = atoi(argv[2]);
  259. }
  260. } else if (!strcmp(argv[1], "off")) {
  261. debug_file = NULL;
  262. }
  263. }
  264. return 0;
  265. }
  266. static void scan_hit_list(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  267. fs_node_t * tty = extra;
  268. fprintf(tty, "%2x:%2x.%d (%4x, %4x:%4x) %s %s\n",
  269. (int)pci_extract_bus(device),
  270. (int)pci_extract_slot(device),
  271. (int)pci_extract_func(device),
  272. (int)pci_find_type(device),
  273. vendorid,
  274. deviceid,
  275. pci_vendor_lookup(vendorid),
  276. pci_device_lookup(vendorid,deviceid));
  277. fprintf(tty, " BAR0: 0x%8x", pci_read_field(device, PCI_BAR0, 4));
  278. fprintf(tty, " BAR1: 0x%8x", pci_read_field(device, PCI_BAR1, 4));
  279. fprintf(tty, " BAR2: 0x%8x", pci_read_field(device, PCI_BAR2, 4));
  280. fprintf(tty, " BAR3: 0x%8x", pci_read_field(device, PCI_BAR3, 4));
  281. fprintf(tty, " BAR4: 0x%8x", pci_read_field(device, PCI_BAR4, 4));
  282. fprintf(tty, " BAR6: 0x%8x\n", pci_read_field(device, PCI_BAR5, 4));
  283. }
  284. static int shell_pci(fs_node_t * tty, int argc, char * argv[]) {
  285. pci_scan(&scan_hit_list, -1, tty);
  286. return 0;
  287. }
  288. static void find_isa_bridge(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  289. if (vendorid == 0x8086 && (deviceid == 0x7000 || deviceid == 0x7110)) {
  290. *((uint32_t *)extra) = device;
  291. }
  292. }
  293. static int shell_frob_piix(fs_node_t * tty, int argc, char * argv[]) {
  294. uint32_t pci_isa = 0;
  295. pci_scan(&find_isa_bridge, -1, &pci_isa);
  296. if (pci_isa) {
  297. fprintf(tty, "Found it.\n");
  298. for (int i = 0; i < 16; ++i) {
  299. fprintf(tty, "%d: 0x%2x\n", i, pci_read_field(pci_isa, 0x60+i, 1));
  300. }
  301. }
  302. return 0;
  303. }
  304. static int shell_uid(fs_node_t * tty, int argc, char * argv[]) {
  305. if (argc < 2) {
  306. fprintf(tty, "uid=%d\n", current_process->user);
  307. } else {
  308. current_process->user = atoi(argv[1]);
  309. }
  310. return 0;
  311. }
  312. char * special_thing = "I am a string from the kernel.\n";
  313. static int shell_mod(fs_node_t * tty, int argc, char * argv[]) {
  314. if (argc < 2) {
  315. fprintf(tty, "%s: expected argument\n", argv[0]);
  316. return 1;
  317. }
  318. fs_node_t * file = kopen(argv[1], 0);
  319. if (!file) {
  320. fprintf(tty, "%s: Error loading module '%s': File not found\n", argv[0], argv[1]);
  321. return 1;
  322. }
  323. close_fs(file);
  324. module_data_t * mod_info = module_load(argv[1]);
  325. if (!mod_info) {
  326. fprintf(tty, "%s: Error loading module '%s'\n", argv[0], argv[1]);
  327. return 1;
  328. }
  329. fprintf(tty, "Module '%s' loaded at 0x%x\n", mod_info->mod_info->name, mod_info->bin_data);
  330. return 0;
  331. }
  332. static int shell_symbols(fs_node_t * tty, int argc, char * argv[]) {
  333. if (argc > 1 && !strcmp(argv[1],"--all")) {
  334. list_t * hash_keys = hashmap_keys(modules_get_symbols());
  335. foreach(_key, hash_keys) {
  336. char * key = (char *)_key->value;
  337. uintptr_t a = (uintptr_t)hashmap_get(modules_get_symbols(), key);
  338. fprintf(tty, "0x%x - %s\n", a, key);
  339. }
  340. free(hash_keys);
  341. } else {
  342. extern char kernel_symbols_start[];
  343. extern char kernel_symbols_end[];
  344. struct ksym {
  345. uintptr_t addr;
  346. char name[];
  347. } * k = (void*)&kernel_symbols_start;
  348. while ((uintptr_t)k < (uintptr_t)&kernel_symbols_end) {
  349. fprintf(tty, "0x%x - %s\n", k->addr, k->name);
  350. k = (void *)((uintptr_t)k + sizeof(uintptr_t) + strlen(k->name) + 1);
  351. }
  352. }
  353. return 0;
  354. }
  355. static int shell_print(fs_node_t * tty, int argc, char * argv[]) {
  356. if (argc < 3) {
  357. fprintf(tty, "print format_string symbol_name\n");
  358. return 1;
  359. }
  360. char * format = argv[1];
  361. char * symbol = argv[2];
  362. int deref = 0;
  363. if (symbol[0] == '*') {
  364. symbol = &symbol[1];
  365. deref = 1;
  366. }
  367. void * addr = hashmap_get(modules_get_symbols(),symbol);
  368. if (!addr) return 1;
  369. if (deref) {
  370. fprintf(tty, format, addr);
  371. } else {
  372. fprintf(tty, format, *((uintptr_t *)addr));
  373. }
  374. fprintf(tty, "\n");
  375. return 0;
  376. }
  377. static int shell_call(fs_node_t * tty, int argc, char * argv[]) {
  378. if (argc < 2) {
  379. fprintf(tty, "call function_name\n");
  380. return 1;
  381. }
  382. char * symbol = argv[1];
  383. void (*addr)(void) = (void (*)(void))(uintptr_t)hashmap_get(modules_get_symbols(),symbol);
  384. if (!addr) return 1;
  385. addr();
  386. return 0;
  387. }
  388. static int shell_modules(fs_node_t * tty, int argc, char * argv[]) {
  389. list_t * hash_keys = hashmap_keys(modules_get_list());
  390. foreach(_key, hash_keys) {
  391. char * key = (char *)_key->value;
  392. module_data_t * mod_info = hashmap_get(modules_get_list(), key);
  393. fprintf(tty, "0x%x {.init=0x%x, .fini=0x%x} %s",
  394. mod_info->bin_data,
  395. mod_info->mod_info->initialize,
  396. mod_info->mod_info->finalize,
  397. mod_info->mod_info->name);
  398. if (mod_info->deps) {
  399. unsigned int i = 0;
  400. fprintf(tty, " Deps: ");
  401. while (i < mod_info->deps_length) {
  402. fprintf(tty, "%s ", &mod_info->deps[i]);
  403. i += strlen(&mod_info->deps[i]) + 1;
  404. }
  405. }
  406. fprintf(tty, "\n");
  407. }
  408. free(hash_keys);
  409. return 0;
  410. }
  411. static int shell_rdtsc(fs_node_t * tty, int argc, char * argv[]) {
  412. uint64_t x;
  413. asm volatile ("rdtsc" : "=A" (x));
  414. fprintf(tty, "0x%x%x\n", (uint32_t)(x >> 32), (uint32_t)(x & 0xFFFFFFFF));
  415. return 0;
  416. }
  417. static int shell_mhz(fs_node_t * tty, int argc, char * argv[]) {
  418. uint64_t x, y;
  419. asm volatile ("rdtsc" : "=A" (x));
  420. unsigned long s, ss;
  421. relative_time(1, 0, &s, &ss);
  422. sleep_until((process_t *)current_process, s, ss);
  423. switch_task(0);
  424. asm volatile ("rdtsc" : "=A" (y));
  425. uint64_t diff = y - x;
  426. uint32_t f = diff >> 15;
  427. uint32_t mhz = f / 30;
  428. fprintf(tty, "%d MHz\n", mhz);
  429. return 0;
  430. }
  431. /*
  432. * Determine the size of a smart terminal that we don't have direct
  433. * termios access to. This is done by sending a cursor-move command
  434. * that will put the cursor into the lower right corner and then
  435. * requesting the cursor position report. We then read and parse
  436. * the position report. In the case where the terminal on the other
  437. * end is actually dumb, we end up waiting for some input and
  438. * then timing out.
  439. * TODO with asyncio support, the timeout should actually work.
  440. * consider also using an alarm (which I also don't have)
  441. */
  442. static void divine_size(fs_node_t * dev, int * width, int * height) {
  443. char tmp[100];
  444. int read = 0;
  445. unsigned long start_tick = timer_ticks;
  446. memset(tmp, 0, sizeof(tmp));
  447. /* Move cursor, Request position, Reset cursor */
  448. tty_set_unbuffered(dev);
  449. fprintf(dev, "\033[1000;1000H\033[6n\033[H");
  450. while (1) {
  451. char buf[1];
  452. int r = read_fs(dev, 0, 1, (unsigned char *)buf);
  453. if (r > 0) {
  454. if (buf[0] != 'R') {
  455. if (read > 1) {
  456. tmp[read-2] = buf[0];
  457. }
  458. read++;
  459. } else {
  460. break;
  461. }
  462. }
  463. if (timer_ticks - start_tick >= 2) {
  464. /*
  465. * We've timed out. This will only be triggered
  466. * when we eventually receive something, though
  467. */
  468. *width = 80;
  469. *height = 23;
  470. /* Clear and return */
  471. fprintf(dev, "\033[J");
  472. tty_set_buffered(dev);
  473. return;
  474. }
  475. }
  476. /* Clear */
  477. fprintf(dev, "\033[J");
  478. /* Break up the result into two strings */
  479. for (unsigned int i = 0; i < strlen(tmp); i++) {
  480. if (tmp[i] == ';') {
  481. tmp[i] = '\0';
  482. break;
  483. }
  484. }
  485. char * h = (char *)((uintptr_t)tmp + strlen(tmp)+1);
  486. /* And then parse it into numbers */
  487. *height = atoi(tmp);
  488. *width = atoi(h);
  489. tty_set_buffered(dev);
  490. }
  491. static int shell_divinesize(fs_node_t * tty, int argc, char * argv[]) {
  492. struct winsize size = {0,0,0,0};
  493. /* Attempt to divine the terminal size. Changing the window size after this will do bad things */
  494. int width, height;
  495. divine_size(tty, &width, &height);
  496. fprintf(tty, "Identified size: %d x %d\n", width, height);
  497. size.ws_row = height;
  498. size.ws_col = width;
  499. ioctl_fs(tty, TIOCSWINSZ, &size);
  500. return 0;
  501. }
  502. static int shell_fix_mouse(fs_node_t * tty, int argc, char * argv[]) {
  503. fs_node_t * mouse = kopen("/dev/mouse", 0);
  504. if (mouse) {
  505. ioctl_fs(mouse, 1, NULL);
  506. close_fs(mouse);
  507. }
  508. return 0;
  509. }
  510. static int shell_mount(fs_node_t * tty, int argc, char * argv[]) {
  511. if (argc < 4) {
  512. fprintf(tty, "Usage: %s type device mountpoint\n", argv[0]);
  513. return 1;
  514. }
  515. return -vfs_mount_type(argv[1], argv[2], argv[3]);
  516. }
  517. static int shell_exit(fs_node_t * tty, int argc, char * argv[]) {
  518. kexit(0);
  519. return 0;
  520. }
  521. static int shell_cursor_off(fs_node_t * tty, int argc, char * argv[]) {
  522. outportb(0x3D4, 14);
  523. outportb(0x3D5, 0xFF);
  524. outportb(0x3D4, 15);
  525. outportb(0x3D5, 0xFF);
  526. return 0;
  527. }
  528. extern pid_t trace_pid;
  529. static int shell_debug_pid(fs_node_t * tty, int argc, char * argv[]) {
  530. trace_pid = atoi(argv[1]);
  531. return 0;
  532. }
  533. static struct shell_command shell_commands[] = {
  534. {"shell", &shell_create_userspace_shell,
  535. "Runs a userspace shell on this tty."},
  536. {"login", &shell_replace_login,
  537. "Replace the debug shell with /bin/login."},
  538. {"echo", &shell_echo,
  539. "Prints arguments."},
  540. {"help", &shell_help,
  541. "Prints a list of possible shell commands and their descriptions."},
  542. {"cd", &shell_cd,
  543. "Change current directory."},
  544. {"ls", &shell_ls,
  545. "List files in current or other directory."},
  546. {"cat", &shell_cat,
  547. "Read a file to the console."},
  548. {"log", &shell_log,
  549. "Configure serial debug logging."},
  550. {"pci", &shell_pci,
  551. "Print PCI devices, as well as their names and BARs."},
  552. {"uid", &shell_uid,
  553. "Change the effective user id of the shell."},
  554. {"mod", &shell_mod,
  555. "[testing] Module loading."},
  556. {"symbols", &shell_symbols,
  557. "Dump symbol table."},
  558. {"debug_pid", &shell_debug_pid,
  559. "Set pid to trace syscalls for."},
  560. {"print", &shell_print,
  561. "[dangerous] Print the value of a symbol using a format string."},
  562. {"call", &shell_call,
  563. "[dangerous] Call a function by name."},
  564. {"modules", &shell_modules,
  565. "Print names and addresses of all loaded modules."},
  566. {"divine-size", &shell_divinesize,
  567. "Attempt to discover TTY size of serial."},
  568. {"fix-mouse", &shell_fix_mouse,
  569. "Attempt to reset mouse device."},
  570. {"mount", &shell_mount,
  571. "Mount a filesystemp."},
  572. {"rdtsc", &shell_rdtsc,
  573. "Read the TSC, if available."},
  574. {"mhz", &shell_mhz,
  575. "Use TSC to determine clock speed."},
  576. {"cursor-off", &shell_cursor_off,
  577. "Disable VGA text mode cursor."},
  578. {"exit", &shell_exit,
  579. "Quit the shell."},
  580. {"piix", &shell_frob_piix,
  581. "frob piix"},
  582. {NULL, NULL, NULL}
  583. };
  584. void debug_shell_install(struct shell_command * sh) {
  585. hashmap_set(shell_commands_map, sh->name, sh);
  586. }
  587. /*
  588. * A TTY object to pass to the tasklets for handling
  589. * serial-tty interaction. This probably shouldn't
  590. * be done as tasklets - TTYs should just be able
  591. * to wrap existing fs_nodes themselves, but that's
  592. * a problem for another day.
  593. */
  594. struct tty_o {
  595. fs_node_t * node;
  596. fs_node_t * tty;
  597. };
  598. /*
  599. * These tasklets handle tty-serial interaction.
  600. */
  601. static void debug_shell_handle_in(void * data, char * name) {
  602. struct tty_o * tty = (struct tty_o *)data;
  603. while (1) {
  604. uint8_t buf[1];
  605. int r = read_fs(tty->tty, 0, 1, (unsigned char *)buf);
  606. write_fs(tty->node, 0, r, buf);
  607. }
  608. }
  609. static void debug_shell_handle_out(void * data, char * name) {
  610. struct tty_o * tty = (struct tty_o *)data;
  611. while (1) {
  612. uint8_t buf[1];
  613. int r = read_fs(tty->node, 0, 1, (unsigned char *)buf);
  614. write_fs(tty->tty, 0, r, buf);
  615. }
  616. }
  617. static void debug_shell_actual(void * data, char * name) {
  618. current_process->image.entry = 0;
  619. fs_node_t * tty = (fs_node_t *)data;
  620. /* Our prompt will include the version number of the current kernel */
  621. char version_number[1024];
  622. sprintf(version_number, __kernel_version_format,
  623. __kernel_version_major,
  624. __kernel_version_minor,
  625. __kernel_version_lower,
  626. __kernel_version_suffix);
  627. /* Initialize the shell commands map */
  628. int retval = 0;
  629. while (1) {
  630. char command[512];
  631. /* Print out the prompt */
  632. if (retval) {
  633. fprintf(tty, "\033[1;34m%s-%s \033[1;31m%d\033[1;34m %s#\033[0m ", __kernel_name, version_number, retval, current_process->wd_name);
  634. } else {
  635. fprintf(tty, "\033[1;34m%s-%s %s#\033[0m ", __kernel_name, version_number, current_process->wd_name);
  636. }
  637. /* Read a line */
  638. debug_shell_readline(tty, command, 511);
  639. char * arg = strdup(command);
  640. char * argv[1024]; /* Command tokens (space-separated elements) */
  641. int argc = tokenize(arg, " ", argv);
  642. if (!argc) continue;
  643. /* Parse the command string */
  644. struct shell_command * sh = hashmap_get(shell_commands_map, argv[0]);
  645. if (sh) {
  646. retval = sh->function(tty, argc, argv);
  647. } else {
  648. fprintf(tty, "Unrecognized command: %s\n", argv[0]);
  649. }
  650. free(arg);
  651. }
  652. }
  653. /*
  654. * Tasklet for managing the kernel serial console.
  655. * This is basically a very simple shell, with access
  656. * to some internal kernel commands, and (eventually)
  657. * debugging routines.
  658. */
  659. static void debug_shell_run(void * data, char * name) {
  660. /*
  661. * We will run on the first serial port.
  662. * TODO detect that this failed
  663. */
  664. fs_node_t * tty = kopen("/dev/ttyS0", 0);
  665. fs_node_t * fs_master;
  666. fs_node_t * fs_slave;
  667. pty_create(NULL, &fs_master, &fs_slave);
  668. /* Attach the serial to the TTY interface */
  669. struct tty_o _tty = {.node = fs_master, .tty = tty};
  670. create_kernel_tasklet(debug_shell_handle_in, "[kttydebug-in]", (void *)&_tty);
  671. create_kernel_tasklet(debug_shell_handle_out, "[kttydebug-out]", (void *)&_tty);
  672. /* Set the device to be the actual TTY slave */
  673. tty = fs_slave;
  674. fs_master->refcount = -1;
  675. fs_slave->refcount = -1;
  676. current_process->fds->entries[0] = tty;
  677. current_process->fds->entries[1] = tty;
  678. current_process->fds->entries[2] = tty;
  679. current_process->fds->length = 3;
  680. tty_set_vintr(tty, 0x02);
  681. fprintf(tty, "\n\n"
  682. "Serial debug console started.\n"
  683. "Type `help` for a list of commands.\n"
  684. "To access a userspace shell, type `shell`.\n"
  685. "Use ^B to send SIGINT instead of ^C.\n"
  686. "\n");
  687. debug_shell_actual(tty, name);
  688. }
  689. int debug_shell_start(void) {
  690. /* Setup shell commands */
  691. shell_commands_map = hashmap_create(10);
  692. struct shell_command * sh = &shell_commands[0];
  693. while (sh->name) {
  694. hashmap_set(shell_commands_map, sh->name, sh);
  695. sh++;
  696. }
  697. debug_hook = debug_shell_actual;
  698. if (args_present("kdebug")) {
  699. int i = create_kernel_tasklet(debug_shell_run, "[kttydebug]", NULL);
  700. debug_print(NOTICE, "Started tasklet with pid=%d", i);
  701. }
  702. return 0;
  703. }
  704. int debug_shell_stop(void) {
  705. debug_print(NOTICE, "Tried to unload debug shell, but debug shell has no real shutdown routine. Don't do that!");
  706. return 0;
  707. }
  708. MODULE_DEF(debugshell, debug_shell_start, debug_shell_stop);
  709. MODULE_DEPENDS(serial);