terminal.c 14 KB

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