debug_sh.c 21 KB

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