terminal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
  2. Commands are spawned as separate processes for preemptive multitasking. */
  3. #include "io.h"
  4. #include "power.h"
  5. #include "bootcon.h"
  6. #include "dbg.h"
  7. #include "idle.h"
  8. #include "kexec.h"
  9. #include "kmalloc.h"
  10. #include "mm.h"
  11. #include "process.h"
  12. #include "sys_fs.h"
  13. #include "terminal.h"
  14. #include "timer.h"
  15. extern void program_reboot_main(void);
  16. extern void program_poweroff_main(void);
  17. extern void program_run_main(const char *filename);
  18. extern void program_display_main(void);
  19. extern void program_gfxbench_main(void);
  20. extern void program_serial_loop_main(const char *arg);
  21. extern void program_serial_recv_main(const char *arg);
  22. extern void program_run_serial_main(const char *arg);
  23. extern void program_run_embedded_main(const char *name);
  24. extern void program_lsusb_main(void);
  25. extern void program_netdev_main(const char *arg);
  26. extern void program_format_main(const char *arg);
  27. extern void program_lspci_main(const char *filter);
  28. extern void program_peek_main(const char *arg);
  29. extern void program_poke_main(const char *arg);
  30. /* ---- Command registry ----
  31. *
  32. * One table drives `help` and the privileged kernel commands that the ring-3
  33. * terminal spawns through SYS_SPAWN_KCMD. The ring-3 shell owns its own
  34. * builtins, Tab completion and PATH lookup. */
  35. enum cmd_kind {
  36. CK_SPAWN = 0, /* spawn, no arguments */
  37. CK_SPAWN_ARG, /* spawn with the raw remainder of the line */
  38. CK_CUSTOM /* handled by a cmd_*() handler below */
  39. };
  40. #define CMD_REQARG 0x01 /* the argument must be non-empty */
  41. #define CMD_BUILTIN 0x04 /* handled inline, before the PATH lookup */
  42. struct kcmd {
  43. const char *name; /* dispatch token */
  44. const char *usage; /* `help` line (and completion name) */
  45. unsigned char kind;
  46. unsigned char flags;
  47. void (*fn)(void); /* spawned entry, no argument */
  48. void (*fn_arg)(const char *); /* spawned entry with an argument */
  49. const char *fixed; /* CK_SPAWN: fixed argument, if any */
  50. int (*custom)(struct term *t, const char *args);
  51. };
  52. /* The terminal's spawn/path helpers are defined later in this file. */
  53. static int cmd_bootlog(struct term *t, const char *args);
  54. static int cmd_dbg(struct term *t, const char *args);
  55. static int cmd_format(struct term *t, const char *args);
  56. static int cmd_help(struct term *t, const char *args);
  57. static int cmd_idle(struct term *t, const char *args);
  58. static int cmd_cpufreq(struct term *t, const char *args);
  59. static int cmd_power(struct term *t, const char *args);
  60. static int cmd_kexec(struct term *t, const char *args);
  61. static int cmd_kill(struct term *t, const char *args);
  62. static int cmd_poweroff(struct term *t, const char *args);
  63. static int cmd_ps(struct term *t, const char *args);
  64. static int cmd_renice(struct term *t, const char *args);
  65. static int cmd_sched(struct term *t, const char *args);
  66. static int cmd_reboot(struct term *t, const char *args);
  67. static int cmd_run(struct term *t, const char *args);
  68. static int cmd_serial(struct term *t, const char *args);
  69. /* Table order defines the `help` layout (printed as two columns). Ring-3
  70. * commands are not listed here: they are files on the PATH. */
  71. static const struct kcmd cmds[] = {
  72. { "help", "help", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
  73. { "bootlog", "bootlog", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_bootlog },
  74. { "run", "run <name>|<file>", CK_CUSTOM, 0, 0, 0, 0, cmd_run },
  75. { "space", "space", CK_SPAWN, 0, 0, program_run_embedded_main, "space", 0 },
  76. { "prog", "prog", CK_SPAWN, 0, 0, program_run_embedded_main, "prog", 0 },
  77. { "reboot", "reboot", CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
  78. { "poweroff", "poweroff", CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
  79. { "display", "display", CK_SPAWN, 0, program_display_main, 0, 0, 0 },
  80. { "gfxbench", "gfxbench", CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
  81. { "lsusb", "lsusb", CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
  82. { "netdev", "netdev [name]", CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
  83. { "format", "format <device> [size]", CK_CUSTOM, 0, 0, 0, 0, cmd_format },
  84. { "lspci", "lspci [id]", CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
  85. { "peek", "peek <addr> [len]", CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
  86. { "poke", "poke <addr> <val>", CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },
  87. { "ps", "ps", CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
  88. { "sched", "sched", CK_CUSTOM, 0, 0, 0, 0, cmd_sched },
  89. { "power", "power", CK_CUSTOM, 0, 0, 0, 0, cmd_power },
  90. { "cpufreq", "cpufreq [auto|min|max|off|n]", CK_CUSTOM, 0, 0, 0, 0, cmd_cpufreq },
  91. { "idle", "idle [auto|hlt|c1e|c3|c6]", CK_CUSTOM, 0, 0, 0, 0, cmd_idle },
  92. { "renice", "renice <pid> <weight>", CK_CUSTOM, CMD_REQARG, 0, 0, 0, cmd_renice },
  93. { "kill", "kill <pid>", CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
  94. { "kexec", "kexec", CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
  95. { "dbg", "dbg", CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
  96. { "serial", "serial test|recv", CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
  97. };
  98. #define NCMDS (sizeof(cmds) / sizeof(cmds[0]))
  99. /* Find the registry entry whose name matches the first word of the line. */
  100. /* Split the line into the command word and the trimmed remainder. */
  101. /* Dispatch one registry entry. Returns 1 when the handler already printed the
  102. * next prompt (clear), 0 otherwise. */
  103. /* `help`: printed as two 32-column columns so it fits the 64-column terminal
  104. * without scrolling. */
  105. static void help_item_pad(const char *s)
  106. {
  107. unsigned int n = 0;
  108. while (s[n]) {
  109. io_putchar(s[n]);
  110. n++;
  111. }
  112. while (n < 32) {
  113. io_putchar(' ');
  114. n++;
  115. }
  116. }
  117. static void help_print(void)
  118. {
  119. unsigned int nitems = NCMDS;
  120. unsigned int half = (nitems + 1) / 2;
  121. unsigned int k;
  122. for (k = 0; k < half; k++) {
  123. if (k + half < nitems) {
  124. help_item_pad(cmds[k].usage);
  125. io_putstr(cmds[k + half].usage);
  126. } else {
  127. io_putstr(cmds[k].usage);
  128. }
  129. io_putchar('\n');
  130. }
  131. }
  132. /* ---- Command handlers that run inline in the terminal ---- */
  133. /* Dump the whole boot console text captured by bootcon_puts(). The ring-3
  134. * terminal keeps scrollback, so no paging is needed; sleep briefly every
  135. * 512 bytes so the terminal can drain its tty ring while we write. */
  136. static int cmd_bootlog(struct term *t, const char *args)
  137. {
  138. const char *p;
  139. unsigned int len, i;
  140. (void)t;
  141. p = bootcon_log(&len);
  142. while (*args == ' ')
  143. args++;
  144. if (args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
  145. args[3] == 'e' && (args[4] == ' ' || args[4] == '\0')) {
  146. const char *path = args + 4;
  147. int r;
  148. while (*path == ' ')
  149. path++;
  150. if (!*path) {
  151. io_putstr("bootlog: usage bootlog save <path>\n");
  152. return 0;
  153. }
  154. r = sys_write_file(path, p, len);
  155. if (r == 0)
  156. io_putstr("bootlog saved\n");
  157. else {
  158. io_putstr("bootlog: save failed (");
  159. io_puthex((unsigned int)r);
  160. io_putstr(")\n");
  161. }
  162. return 0;
  163. }
  164. for (i = 0; i < len; i++) {
  165. io_putchar(p[i]);
  166. if ((i & 511u) == 511u)
  167. process_sleep_ms(1);
  168. }
  169. if (bootcon_log_truncated())
  170. io_putstr("\n[boot log truncated]\n");
  171. return 0;
  172. }
  173. static int cmd_dbg(struct term *t, const char *args)
  174. {
  175. (void)t;
  176. (void)args;
  177. program_dbg_main();
  178. return 0;
  179. }
  180. static int cmd_format(struct term *t, const char *args)
  181. {
  182. (void)t;
  183. program_format_main(args);
  184. return 0;
  185. }
  186. /* `path` builtin: show or set the command search path. */
  187. /* List the command files found on the search path. */
  188. static void help_print_path(struct term *t)
  189. {
  190. const char *p = t->path;
  191. while (*p) {
  192. char dir[VFS_PATH_MAX];
  193. unsigned int n = 0, col = 0, i;
  194. while (*p && *p != ':' && n + 1 < sizeof(dir))
  195. dir[n++] = *p++;
  196. dir[n] = '\0';
  197. if (*p == ':')
  198. p++;
  199. if (dir[0] == '\0')
  200. continue;
  201. io_putstr(dir);
  202. io_putstr(":\n");
  203. for (i = 0; ; i++) {
  204. struct vfs_dirent d;
  205. unsigned int len = 0;
  206. int r = vfs_list_dir(dir, i, &d);
  207. if (r != 1)
  208. break;
  209. if (d.is_dir)
  210. continue;
  211. while (d.name[len])
  212. len++;
  213. if (col > 0 && col + 2 + len > t->screen.cols) {
  214. io_putchar('\n');
  215. col = 0;
  216. }
  217. if (col > 0) {
  218. io_putstr(" ");
  219. col += 2;
  220. }
  221. io_putstr(d.name);
  222. col += len;
  223. }
  224. io_putchar('\n');
  225. }
  226. }
  227. static int cmd_help(struct term *t, const char *args)
  228. {
  229. (void)args;
  230. io_putstr("shell builtins: cd path clear\n");
  231. help_print();
  232. help_print_path(t);
  233. return 0;
  234. }
  235. static int cmd_kexec(struct term *t, const char *args)
  236. {
  237. (void)t;
  238. (void)args;
  239. if (kexec_busy) {
  240. io_putstr("kexec: already receiving\n");
  241. return 0;
  242. }
  243. kexec_busy = 1;
  244. if (process_create("kexec", program_kexec_main) < 0) {
  245. kexec_busy = 0;
  246. io_putstr("error: too many processes\n");
  247. }
  248. return 0;
  249. }
  250. static int cmd_kill(struct term *t, const char *args)
  251. {
  252. unsigned int pid = 0;
  253. int has_digit = 0;
  254. (void)t;
  255. while (*args >= '0' && *args <= '9') {
  256. pid = pid * 10 + (unsigned int)(*args - '0');
  257. args++;
  258. has_digit = 1;
  259. }
  260. while (*args == ' ')
  261. args++;
  262. if (!has_digit || *args) {
  263. io_putstr("kill: usage kill <pid>\n");
  264. return 0;
  265. }
  266. if (process_kill(pid) < 0)
  267. io_putstr("kill: no such process\n");
  268. else
  269. io_putstr("killed\n");
  270. return 0;
  271. }
  272. static int cmd_poweroff(struct term *t, const char *args)
  273. {
  274. (void)t;
  275. (void)args;
  276. program_poweroff_main();
  277. return 0;
  278. }
  279. static int cmd_ps(struct term *t, const char *args)
  280. {
  281. (void)t;
  282. (void)args;
  283. process_ps();
  284. return 0;
  285. }
  286. static int cmd_sched(struct term *t, const char *args)
  287. {
  288. (void)t;
  289. (void)args;
  290. sched_print();
  291. return 0;
  292. }
  293. static int arg_is(const char *args, const char *word)
  294. {
  295. unsigned int i = 0;
  296. while (word[i] && args[i] == word[i])
  297. i++;
  298. return word[i] == '\0' && args[i] == '\0';
  299. }
  300. static int cmd_idle(struct term *t, const char *args)
  301. {
  302. (void)t;
  303. if (!args[0] || arg_is(args, "status")) {
  304. io_putstr("idle ");
  305. io_putstr(idle_mode_name(idle_mode()));
  306. if (!idle_mwait_supported())
  307. io_putstr(" (mwait unsupported)");
  308. io_putchar('\n');
  309. return 0;
  310. }
  311. if (arg_is(args, "auto")) {
  312. idle_set_mode(IDLE_AUTO);
  313. io_putstr("idle auto\n");
  314. return 0;
  315. }
  316. if (arg_is(args, "hlt")) {
  317. idle_set_mode(IDLE_C1);
  318. io_putstr("idle hlt\n");
  319. return 0;
  320. }
  321. {
  322. int mode = -1;
  323. if (arg_is(args, "c1e"))
  324. mode = IDLE_C1E;
  325. else if (arg_is(args, "c3"))
  326. mode = IDLE_C3;
  327. else if (arg_is(args, "c6"))
  328. mode = IDLE_C6;
  329. else if (arg_is(args, "c3ap"))
  330. mode = IDLE_C3_AP;
  331. else if (arg_is(args, "c6ap"))
  332. mode = IDLE_C6_AP;
  333. if (mode >= 0) {
  334. if (idle_set_mode(mode) < 0) {
  335. io_putstr("idle: mwait not available\n");
  336. return 0;
  337. }
  338. io_putstr("idle ");
  339. io_putstr(idle_mode_name(mode));
  340. io_putstr(" (test the keyboard; power-cycle if it hangs)\n");
  341. return 0;
  342. }
  343. }
  344. io_putstr("idle: usage idle [auto|hlt|c1e|c3|c6|c3ap|c6ap]\n");
  345. return 0;
  346. }
  347. static int cmd_power(struct term *t, const char *args)
  348. {
  349. (void)t;
  350. (void)args;
  351. power_print();
  352. return 0;
  353. }
  354. static int cmd_cpufreq(struct term *t, const char *args)
  355. {
  356. (void)t;
  357. power_cpufreq_command(args);
  358. return 0;
  359. }
  360. static int cmd_renice(struct term *t, const char *args)
  361. {
  362. unsigned int pid = 0, weight = 0;
  363. int have_pid = 0, have_weight = 0;
  364. (void)t;
  365. while (*args >= '0' && *args <= '9') {
  366. pid = pid * 10 + (unsigned int)(*args - '0');
  367. args++;
  368. have_pid = 1;
  369. }
  370. while (*args == ' ')
  371. args++;
  372. while (*args >= '0' && *args <= '9') {
  373. weight = weight * 10 + (unsigned int)(*args - '0');
  374. args++;
  375. have_weight = 1;
  376. }
  377. while (*args == ' ')
  378. args++;
  379. if (!have_pid || !have_weight || *args ||
  380. weight < 1 || weight > SCHED_WEIGHT_MAX) {
  381. io_putstr("renice: usage renice <pid> <1..16>\n");
  382. return 0;
  383. }
  384. if (process_set_weight(pid, weight) < 0)
  385. io_putstr("renice: no such process\n");
  386. else
  387. io_putstr("weight changed\n");
  388. return 0;
  389. }
  390. static int cmd_reboot(struct term *t, const char *args)
  391. {
  392. (void)t;
  393. (void)args;
  394. program_reboot_main();
  395. return 0;
  396. }
  397. static int cmd_run(struct term *t, const char *args)
  398. {
  399. (void)t;
  400. if (args[0] == 's' && args[1] == 'e' && args[2] == 'r' &&
  401. args[3] == 'i' && args[4] == 'a' && args[5] == 'l' &&
  402. args[6] == '\0') {
  403. if (process_create_arg("run_serial", program_run_serial_main, "serial") < 0)
  404. io_putstr("error: too many processes\n");
  405. } else {
  406. io_putstr("run: not available in windowed mode yet\n");
  407. }
  408. return 0;
  409. }
  410. static int cmd_serial(struct term *t, const char *args)
  411. {
  412. (void)t;
  413. if (args[0] == 't' && args[1] == 'e' && args[2] == 's' && args[3] == 't' &&
  414. args[4] == '\0') {
  415. if (process_create_arg("serial_loop", program_serial_loop_main, "test") < 0)
  416. io_putstr("error: too many processes\n");
  417. } else if (args[0] == 'r' && args[1] == 'e' && args[2] == 'c' &&
  418. args[3] == 'v' && args[4] == '\0') {
  419. program_serial_recv_main("recv");
  420. } else {
  421. io_putstr("serial: usage serial test|recv\n");
  422. }
  423. return 0;
  424. }
  425. struct kcmd_ctx {
  426. char name[16];
  427. char args[VFS_PATH_MAX];
  428. };
  429. static const struct kcmd *kcmd_find(const char *name)
  430. {
  431. unsigned int i;
  432. for (i = 0; i < NCMDS; i++)
  433. if (cmds[i].name[0] == name[0]) {
  434. unsigned int j = 0;
  435. while (cmds[i].name[j] && name[j] && cmds[i].name[j] == name[j])
  436. j++;
  437. if (!cmds[i].name[j] && !name[j])
  438. return &cmds[i];
  439. }
  440. return 0;
  441. }
  442. static void kcmd_entry(void *arg)
  443. {
  444. struct kcmd_ctx *ctx = (struct kcmd_ctx *)arg;
  445. const struct kcmd *c = kcmd_find(ctx->name);
  446. if (c) {
  447. if (c->kind == CK_CUSTOM) {
  448. struct term *t = (struct term *)mm_alloc_pages(2);
  449. unsigned int i;
  450. if (!t) {
  451. io_putstr("kcmd: out of memory\n");
  452. } else {
  453. unsigned char *p = (unsigned char *)t;
  454. for (i = 0; i < 2 * PAGE_SIZE; i++)
  455. p[i] = 0;
  456. /* Kernel-command output has no cell screen; help uses
  457. * this width to lay out its columns on the tty. */
  458. t->screen.cols = 64;
  459. t->screen.rows = 32;
  460. for (i = 0; i + 1 < sizeof(t->cwd) &&
  461. current_proc()->cwd[i]; i++)
  462. t->cwd[i] = current_proc()->cwd[i];
  463. t->cwd[i] = '\0';
  464. {
  465. const char *def = "/ramfs/bin";
  466. for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
  467. t->path[i] = def[i];
  468. t->path[i] = '\0';
  469. }
  470. c->custom(t, ctx->args);
  471. mm_free_page((unsigned int)t);
  472. mm_free_page((unsigned int)t + PAGE_SIZE);
  473. }
  474. } else if (c->kind == CK_SPAWN) {
  475. if (c->fixed)
  476. c->fn_arg(c->fixed);
  477. else
  478. c->fn();
  479. } else if (c->kind == CK_SPAWN_ARG) {
  480. c->fn_arg(ctx->args);
  481. }
  482. } else {
  483. io_putstr(ctx->name);
  484. io_putstr(": no such kernel command\n");
  485. }
  486. kfree(ctx);
  487. process_exit();
  488. }
  489. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
  490. {
  491. const struct kcmd *c = kcmd_find(name);
  492. struct kcmd_ctx *ctx;
  493. unsigned int i;
  494. int pid;
  495. if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help &&
  496. c->custom != cmd_bootlog))
  497. return -1;
  498. ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
  499. if (!ctx)
  500. return -1;
  501. for (i = 0; i + 1 < sizeof(ctx->name) && name[i]; i++)
  502. ctx->name[i] = name[i];
  503. ctx->name[i] = '\0';
  504. for (i = 0; i + 1 < sizeof(ctx->args) && args[i]; i++)
  505. ctx->args[i] = args[i];
  506. ctx->args[i] = '\0';
  507. pid = process_create_ptr("kcmd", kcmd_entry, ctx);
  508. if (pid < 0) {
  509. kfree(ctx);
  510. return -1;
  511. }
  512. if (tty)
  513. process_set_tty((unsigned int)pid, (int)tty);
  514. return pid;
  515. }