terminal.c 14 KB

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