terminal.c 15 KB

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