2014-06-08 10:51:01 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
2014-06-08 10:58:31 +04:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2014 Kevin Lange
|
2014-06-08 10:58:31 +04:00
|
|
|
*
|
2013-11-28 07:11:58 +04:00
|
|
|
* Kernel Debug Shell
|
|
|
|
*/
|
|
|
|
#include <system.h>
|
|
|
|
#include <fs.h>
|
2014-04-06 03:36:17 +04:00
|
|
|
#include <printf.h>
|
2013-11-28 07:11:58 +04:00
|
|
|
#include <logging.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <version.h>
|
2013-11-28 10:24:58 +04:00
|
|
|
#include <termios.h>
|
2013-12-02 09:19:23 +04:00
|
|
|
#include <tokenize.h>
|
|
|
|
#include <hashmap.h>
|
2013-12-13 11:40:52 +04:00
|
|
|
#include <pci.h>
|
2014-01-10 12:45:59 +04:00
|
|
|
#include <pipe.h>
|
2014-03-10 06:36:28 +04:00
|
|
|
#include <elf.h>
|
2014-03-12 10:56:08 +04:00
|
|
|
#include <module.h>
|
2013-11-28 10:24:58 +04:00
|
|
|
|
2014-03-15 10:37:42 +04:00
|
|
|
#include <mod/shell.h>
|
2013-12-02 07:37:13 +04:00
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/*
|
|
|
|
* This is basically the same as a userspace buffered/unbuffered
|
|
|
|
* termio call. These are the same sorts of things I would use in
|
|
|
|
* a text editor in userspace, but with the internal kernel calls
|
|
|
|
* rather than system calls.
|
|
|
|
*/
|
2013-11-28 10:24:58 +04:00
|
|
|
static struct termios old;
|
|
|
|
|
2014-05-20 06:39:04 +04:00
|
|
|
void tty_set_unbuffered(fs_node_t * dev) {
|
2013-11-28 10:24:58 +04:00
|
|
|
ioctl_fs(dev, TCGETS, &old);
|
|
|
|
struct termios new = old;
|
|
|
|
new.c_lflag &= (~ICANON & ~ECHO);
|
|
|
|
ioctl_fs(dev, TCSETSF, &new);
|
|
|
|
}
|
|
|
|
|
2014-05-20 06:39:04 +04:00
|
|
|
void tty_set_buffered(fs_node_t * dev) {
|
2013-11-28 10:24:58 +04:00
|
|
|
ioctl_fs(dev, TCSETSF, &old);
|
|
|
|
}
|
|
|
|
|
2014-05-30 09:01:53 +04:00
|
|
|
void tty_set_vintr(fs_node_t * dev, char vintr) {
|
|
|
|
struct termios tmp;
|
|
|
|
ioctl_fs(dev, TCGETS, &tmp);
|
|
|
|
tmp.c_cc[VINTR] = vintr;
|
|
|
|
ioctl_fs(dev, TCSETSF, &tmp);
|
|
|
|
}
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/*
|
|
|
|
* Quick readline implementation.
|
|
|
|
*
|
|
|
|
* Most of these TODOs are things I've done already in older code:
|
|
|
|
* TODO tabcompletion would be nice
|
|
|
|
* TODO history is also nice
|
|
|
|
*/
|
2014-05-17 23:26:14 +04:00
|
|
|
int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
|
2013-11-28 07:11:58 +04:00
|
|
|
int read = 0;
|
2014-05-20 06:39:04 +04:00
|
|
|
tty_set_unbuffered(dev);
|
2013-11-28 07:11:58 +04:00
|
|
|
while (read < max) {
|
|
|
|
uint8_t buf[1];
|
|
|
|
int r = read_fs(dev, 0, 1, (unsigned char *)buf);
|
|
|
|
if (!r) {
|
|
|
|
debug_print(WARNING, "Read nothing?");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
linebuf[read] = buf[0];
|
2013-11-28 09:21:39 +04:00
|
|
|
if (buf[0] == '\n') {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "\n");
|
2013-11-28 07:11:58 +04:00
|
|
|
linebuf[read] = 0;
|
|
|
|
break;
|
2013-11-28 09:21:39 +04:00
|
|
|
} else if (buf[0] == 0x08) {
|
2013-11-28 07:11:58 +04:00
|
|
|
if (read > 0) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "\010 \010");
|
2013-11-28 07:11:58 +04:00
|
|
|
read--;
|
|
|
|
linebuf[read] = 0;
|
|
|
|
}
|
2014-03-27 08:41:58 +04:00
|
|
|
} else if (buf[0] < ' ') {
|
|
|
|
switch (buf[0]) {
|
2014-05-30 09:01:53 +04:00
|
|
|
case 0x04:
|
|
|
|
if (read == 0) {
|
|
|
|
fprintf(dev, "exit\n");
|
|
|
|
sprintf(linebuf, "exit");
|
|
|
|
return strlen(linebuf);
|
|
|
|
}
|
|
|
|
break;
|
2014-03-27 08:41:58 +04:00
|
|
|
case 0x0C: /* ^L */
|
|
|
|
/* Should reset display here */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "%c", buf[0]);
|
2014-03-27 08:41:58 +04:00
|
|
|
read += r;
|
2013-11-28 07:11:58 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-20 06:39:04 +04:00
|
|
|
tty_set_buffered(dev);
|
2013-11-28 07:11:58 +04:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/*
|
|
|
|
* Tasklet for running a userspace application.
|
|
|
|
*/
|
2014-03-15 10:17:59 +04:00
|
|
|
static void debug_shell_run_sh(void * data, char * name) {
|
2013-11-28 09:21:39 +04:00
|
|
|
|
|
|
|
char * argv[] = {
|
2014-06-08 07:44:13 +04:00
|
|
|
data,
|
2013-11-28 09:21:39 +04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int argc = 0;
|
|
|
|
while (argv[argc]) {
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
system(argv[0], argc, argv); /* Run shell */
|
|
|
|
|
|
|
|
task_exit(42);
|
|
|
|
}
|
|
|
|
|
2014-03-15 10:17:59 +04:00
|
|
|
static hashmap_t * shell_commands_map = NULL;
|
2013-11-30 12:09:04 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Shell commands
|
|
|
|
*/
|
|
|
|
static int shell_create_userspace_shell(fs_node_t * tty, int argc, char * argv[]) {
|
2014-06-08 07:44:13 +04:00
|
|
|
int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/sh");
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "Shell started with pid = %d\n", pid);
|
2014-04-27 12:37:33 +04:00
|
|
|
int status;
|
|
|
|
waitpid(pid,&status,0);
|
|
|
|
return status;
|
2013-11-30 12:09:04 +04:00
|
|
|
}
|
|
|
|
|
2014-06-08 07:44:13 +04:00
|
|
|
static int shell_replace_login(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
/* We need to fork to get a clean task space */
|
|
|
|
create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/login");
|
|
|
|
/* Then exit the shell process */
|
|
|
|
task_exit(0);
|
|
|
|
/* unreachable */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
static int shell_echo(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "%s ", argv[i]);
|
2013-11-30 12:09:04 +04:00
|
|
|
}
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "\n");
|
2013-11-30 12:09:04 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-12 05:29:28 +04:00
|
|
|
static int dumb_strcmp(void * a, void *b) {
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumb_sort(void ** list, size_t length, int (*compare)(void*,void*)) {
|
|
|
|
for (unsigned int i = 0; i < length-1; ++i) {
|
|
|
|
for (unsigned int j = 0; j < length-1; ++j) {
|
|
|
|
if (compare(list[j], list[j+1]) > 0) {
|
|
|
|
void * t = list[j+1];
|
|
|
|
list[j+1] = list[j];
|
|
|
|
list[j] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_spaces(fs_node_t * tty, int num_spaces) {
|
|
|
|
for (int i = 0; i < num_spaces; ++i) {
|
|
|
|
fprintf(tty, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
static int shell_help(fs_node_t * tty, int argc, char * argv[]) {
|
2013-12-02 11:29:40 +04:00
|
|
|
list_t * hash_keys = hashmap_keys(shell_commands_map);
|
|
|
|
|
2014-04-12 05:29:28 +04:00
|
|
|
char ** keys = malloc(sizeof(char *) * hash_keys->length);
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned int max_width = 0;
|
|
|
|
|
2013-12-02 11:29:40 +04:00
|
|
|
foreach(_key, hash_keys) {
|
|
|
|
char * key = (char *)_key->value;
|
2014-04-12 05:29:28 +04:00
|
|
|
keys[i] = key;
|
|
|
|
i++;
|
|
|
|
if (strlen(key) > max_width) {
|
|
|
|
max_width = strlen(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dumb_sort((void **)keys, hash_keys->length, &dumb_strcmp);
|
2013-12-02 11:29:40 +04:00
|
|
|
|
2014-04-12 05:29:28 +04:00
|
|
|
for (i = 0; i < hash_keys->length; ++i) {
|
|
|
|
struct shell_command * c = hashmap_get(shell_commands_map, keys[i]);
|
|
|
|
fprintf(tty, "\033[1;32m%s\033[0m ", c->name);
|
|
|
|
print_spaces(tty, max_width- strlen(c->name));
|
|
|
|
fprintf(tty, "- %s\n", c->description);
|
2013-11-30 12:09:04 +04:00
|
|
|
}
|
2013-12-02 11:29:40 +04:00
|
|
|
|
2014-04-12 05:29:28 +04:00
|
|
|
free(keys);
|
2013-12-02 11:29:40 +04:00
|
|
|
list_free(hash_keys);
|
|
|
|
free(hash_keys);
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-01 10:27:45 +04:00
|
|
|
static int shell_cd(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
if (argc < 2) {
|
2014-06-10 07:13:11 +04:00
|
|
|
return 1;
|
2013-12-01 10:27:45 +04:00
|
|
|
}
|
|
|
|
char * newdir = argv[1];
|
|
|
|
char * path = canonicalize_path(current_process->wd_name, newdir);
|
|
|
|
fs_node_t * chd = kopen(path, 0);
|
|
|
|
if (chd) {
|
|
|
|
if ((chd->flags & FS_DIRECTORY) == 0) {
|
2014-06-10 07:13:11 +04:00
|
|
|
return 1;
|
2013-12-01 10:27:45 +04:00
|
|
|
}
|
2014-06-06 09:22:17 +04:00
|
|
|
close_fs(chd);
|
2013-12-01 10:27:45 +04:00
|
|
|
free(current_process->wd_name);
|
|
|
|
current_process->wd_name = malloc(strlen(path) + 1);
|
|
|
|
memcpy(current_process->wd_name, path, strlen(path) + 1);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2014-06-10 07:13:11 +04:00
|
|
|
return 1;
|
2013-12-01 10:27:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_ls(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
/* Okay, we're going to take the working directory... */
|
|
|
|
fs_node_t * wd = kopen(current_process->wd_name, 0);
|
|
|
|
uint32_t index = 0;
|
|
|
|
struct dirent * kentry = readdir_fs(wd, index);
|
|
|
|
while (kentry) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "%s\n", kentry->name);
|
2014-04-12 05:29:28 +04:00
|
|
|
free(kentry);
|
2013-12-01 10:27:45 +04:00
|
|
|
|
|
|
|
index++;
|
|
|
|
kentry = readdir_fs(wd, index);
|
|
|
|
}
|
|
|
|
close_fs(wd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-15 01:47:18 +04:00
|
|
|
static int shell_log(fs_node_t * tty, int argc, char * argv[]) {
|
2013-12-02 09:19:23 +04:00
|
|
|
if (argc < 2) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "Log level is currently %d.\n", debug_level);
|
|
|
|
fprintf(tty, "Serial logging is %s.\n", !!debug_file ? "enabled" : "disabled");
|
|
|
|
fprintf(tty, "Usage: log [on|off] [<level>]\n");
|
2013-12-02 09:19:23 +04:00
|
|
|
} else {
|
2015-05-19 07:05:52 +03:00
|
|
|
if (!strcmp(argv[1], "direct")) {
|
|
|
|
debug_file = kopen("/dev/ttyS0", 0);
|
|
|
|
if (argc > 2) {
|
|
|
|
debug_level = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[1], "on")) {
|
2014-04-06 03:12:09 +04:00
|
|
|
debug_file = tty;
|
2013-12-15 01:47:18 +04:00
|
|
|
if (argc > 2) {
|
|
|
|
debug_level = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[1], "off")) {
|
2014-04-06 03:12:09 +04:00
|
|
|
debug_file = NULL;
|
2013-12-15 01:47:18 +04:00
|
|
|
}
|
2013-12-02 09:19:23 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-11 10:34:55 +04:00
|
|
|
static void scan_hit_list(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
|
2013-12-13 11:40:52 +04:00
|
|
|
|
2014-04-11 10:34:55 +04:00
|
|
|
fs_node_t * tty = extra;
|
2013-12-13 11:40:52 +04:00
|
|
|
|
2014-04-11 09:23:36 +04:00
|
|
|
fprintf(tty, "%2x:%2x.%d (%4x, %4x:%4x) %s %s\n",
|
2013-12-13 11:40:52 +04:00
|
|
|
(int)pci_extract_bus(device),
|
|
|
|
(int)pci_extract_slot(device),
|
|
|
|
(int)pci_extract_func(device),
|
|
|
|
(int)pci_find_type(device),
|
|
|
|
vendorid,
|
|
|
|
deviceid,
|
|
|
|
pci_vendor_lookup(vendorid),
|
|
|
|
pci_device_lookup(vendorid,deviceid));
|
|
|
|
|
2014-04-11 09:23:36 +04:00
|
|
|
fprintf(tty, " BAR0: 0x%8x\n", pci_read_field(device, PCI_BAR0, 4));
|
|
|
|
fprintf(tty, " BAR1: 0x%8x\n", pci_read_field(device, PCI_BAR1, 4));
|
|
|
|
fprintf(tty, " BAR2: 0x%8x\n", pci_read_field(device, PCI_BAR2, 4));
|
|
|
|
fprintf(tty, " BAR3: 0x%8x\n", pci_read_field(device, PCI_BAR3, 4));
|
|
|
|
fprintf(tty, " BAR4: 0x%8x\n", pci_read_field(device, PCI_BAR4, 4));
|
|
|
|
fprintf(tty, " BAR6: 0x%8x\n", pci_read_field(device, PCI_BAR5, 4));
|
2013-12-13 11:40:52 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_pci(fs_node_t * tty, int argc, char * argv[]) {
|
2014-04-11 10:34:55 +04:00
|
|
|
pci_scan(&scan_hit_list, -1, tty);
|
2013-12-13 11:40:52 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-15 01:47:18 +04:00
|
|
|
static int shell_uid(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
if (argc < 2) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "uid=%d\n", current_process->user);
|
2013-12-15 01:47:18 +04:00
|
|
|
} else {
|
|
|
|
current_process->user = atoi(argv[1]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 09:37:49 +04:00
|
|
|
char * special_thing = "I am a string from the kernel.\n";
|
2014-03-10 06:36:28 +04:00
|
|
|
|
|
|
|
static int shell_mod(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
if (argc < 2) {
|
2014-06-08 06:48:37 +04:00
|
|
|
fprintf(tty, "%s: expected argument\n", argv[0]);
|
2014-03-10 06:36:28 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fs_node_t * file = kopen(argv[1], 0);
|
|
|
|
if (!file) {
|
2014-06-08 06:48:37 +04:00
|
|
|
fprintf(tty, "%s: Error loading module '%s': File not found\n", argv[0], argv[1]);
|
2014-03-10 06:36:28 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2014-06-08 06:48:37 +04:00
|
|
|
close_fs(file);
|
2014-03-10 06:36:28 +04:00
|
|
|
|
2014-03-17 01:39:39 +04:00
|
|
|
module_data_t * mod_info = module_load(argv[1]);
|
2014-03-12 11:18:55 +04:00
|
|
|
if (!mod_info) {
|
2014-06-08 06:48:37 +04:00
|
|
|
fprintf(tty, "%s: Error loading module '%s'\n", argv[0], argv[1]);
|
2014-03-12 11:18:55 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-08 06:48:37 +04:00
|
|
|
fprintf(tty, "Module '%s' loaded at 0x%x\n", mod_info->mod_info->name, mod_info->bin_data);
|
2014-03-10 09:37:49 +04:00
|
|
|
|
2014-03-10 06:36:28 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_symbols(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
extern char kernel_symbols_start[];
|
|
|
|
extern char kernel_symbols_end[];
|
|
|
|
|
|
|
|
struct ksym {
|
|
|
|
uintptr_t addr;
|
|
|
|
char name[];
|
|
|
|
} * k = (void*)&kernel_symbols_start;
|
|
|
|
|
|
|
|
while ((uintptr_t)k < (uintptr_t)&kernel_symbols_end) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "0x%x - %s\n", k->addr, k->name);
|
2014-03-10 06:36:28 +04:00
|
|
|
k = (void *)((uintptr_t)k + sizeof(uintptr_t) + strlen(k->name) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_print(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
|
|
|
|
if (argc < 3) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "print format_string symbol_name\n");
|
2014-03-10 06:36:28 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * format = argv[1];
|
|
|
|
char * symbol = argv[2];
|
|
|
|
int deref = 0;
|
|
|
|
|
|
|
|
if (symbol[0] == '*') {
|
|
|
|
symbol = &symbol[1];
|
|
|
|
deref = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern char kernel_symbols_start[];
|
|
|
|
extern char kernel_symbols_end[];
|
|
|
|
|
|
|
|
struct ksym {
|
|
|
|
uintptr_t addr;
|
|
|
|
char name[];
|
|
|
|
} * k = (void*)&kernel_symbols_start;
|
|
|
|
|
|
|
|
while ((uintptr_t)k < (uintptr_t)&kernel_symbols_end) {
|
|
|
|
if (!strcmp(symbol, k->name)) {
|
|
|
|
if (deref) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, format, k->addr);
|
2014-03-10 06:36:28 +04:00
|
|
|
} else {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, format, *((uintptr_t *)k->addr));
|
2014-03-10 06:36:28 +04:00
|
|
|
}
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "\n");
|
2014-03-10 06:36:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
k = (void *)((uintptr_t)k + sizeof(uintptr_t) + strlen(k->name) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-15 11:03:48 +04:00
|
|
|
static int shell_modules(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
list_t * hash_keys = hashmap_keys(modules_get_list());
|
|
|
|
foreach(_key, hash_keys) {
|
|
|
|
char * key = (char *)_key->value;
|
2014-03-17 01:39:39 +04:00
|
|
|
module_data_t * mod_info = hashmap_get(modules_get_list(), key);
|
2014-03-15 11:03:48 +04:00
|
|
|
|
2014-04-12 11:22:08 +04:00
|
|
|
fprintf(tty, "0x%x {.init=0x%x, .fini=0x%x} %s",
|
|
|
|
mod_info->bin_data,
|
|
|
|
mod_info->mod_info->initialize,
|
|
|
|
mod_info->mod_info->finalize,
|
|
|
|
mod_info->mod_info->name);
|
2014-03-19 07:11:56 +04:00
|
|
|
|
|
|
|
if (mod_info->deps) {
|
|
|
|
unsigned int i = 0;
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, " Deps: ");
|
2014-03-19 07:11:56 +04:00
|
|
|
while (i < mod_info->deps_length) {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "%s ", &mod_info->deps[i]);
|
2014-03-19 07:11:56 +04:00
|
|
|
i += strlen(&mod_info->deps[i]) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "\n");
|
2014-03-15 11:03:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-15 09:05:07 +03:00
|
|
|
static int shell_rdtsc(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
uint64_t x;
|
|
|
|
asm volatile (".byte 0x0f, 0x31" : "=A" (x));
|
|
|
|
|
|
|
|
fprintf(tty, "0x%x%x\n", (uint32_t)(x >> 32), (uint32_t)(x & 0xFFFFFFFF));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_mhz(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
|
|
|
|
uint64_t x, y;
|
|
|
|
|
|
|
|
asm volatile (".byte 0x0f, 0x31" : "=A" (x));
|
|
|
|
|
|
|
|
unsigned long s, ss;
|
|
|
|
relative_time(1, 0, &s, &ss);
|
|
|
|
sleep_until((process_t *)current_process, s, ss);
|
|
|
|
switch_task(0);
|
|
|
|
|
|
|
|
asm volatile (".byte 0x0f, 0x31" : "=A" (y));
|
|
|
|
|
|
|
|
uint64_t diff = y - x;
|
|
|
|
uint32_t f = diff >> 15;
|
|
|
|
uint32_t mhz = f / 30;
|
|
|
|
|
|
|
|
fprintf(tty, "%d MHz\n", mhz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-24 07:18:11 +04:00
|
|
|
/*
|
|
|
|
* Determine the size of a smart terminal that we don't have direct
|
|
|
|
* termios access to. This is done by sending a cursor-move command
|
|
|
|
* that will put the cursor into the lower right corner and then
|
|
|
|
* requesting the cursor position report. We then read and parse
|
|
|
|
* the position report. In the case where the terminal on the other
|
|
|
|
* end is actually dumb, we end up waiting for some input and
|
|
|
|
* then timing out.
|
|
|
|
* TODO with asyncio support, the timeout should actually work.
|
|
|
|
* consider also using an alarm (which I also don't have)
|
|
|
|
*/
|
|
|
|
static void divine_size(fs_node_t * dev, int * width, int * height) {
|
|
|
|
char tmp[100];
|
|
|
|
int read = 0;
|
|
|
|
unsigned long start_tick = timer_ticks;
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
/* Move cursor, Request position, Reset cursor */
|
2014-05-20 06:39:04 +04:00
|
|
|
tty_set_unbuffered(dev);
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "\033[1000;1000H\033[6n\033[H");
|
2014-03-24 07:18:11 +04:00
|
|
|
while (1) {
|
|
|
|
char buf[1];
|
|
|
|
int r = read_fs(dev, 0, 1, (unsigned char *)buf);
|
|
|
|
if (r > 0) {
|
|
|
|
if (buf[0] != 'R') {
|
|
|
|
if (read > 1) {
|
|
|
|
tmp[read-2] = buf[0];
|
|
|
|
}
|
|
|
|
read++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer_ticks - start_tick >= 2) {
|
|
|
|
/*
|
|
|
|
* We've timed out. This will only be triggered
|
|
|
|
* when we eventually receive something, though
|
|
|
|
*/
|
|
|
|
*width = 80;
|
|
|
|
*height = 23;
|
|
|
|
/* Clear and return */
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "\033[J");
|
2014-06-06 22:37:47 +04:00
|
|
|
tty_set_buffered(dev);
|
2014-03-24 07:18:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Clear */
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(dev, "\033[J");
|
2014-03-24 07:18:11 +04:00
|
|
|
/* Break up the result into two strings */
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < strlen(tmp); i++) {
|
|
|
|
if (tmp[i] == ';') {
|
|
|
|
tmp[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char * h = (char *)((uintptr_t)tmp + strlen(tmp)+1);
|
|
|
|
/* And then parse it into numbers */
|
|
|
|
*height = atoi(tmp);
|
|
|
|
*width = atoi(h);
|
2014-06-06 22:37:47 +04:00
|
|
|
tty_set_buffered(dev);
|
2014-03-24 07:18:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int shell_divinesize(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
struct winsize size = {0,0,0,0};
|
|
|
|
|
|
|
|
/* Attempt to divine the terminal size. Changing the window size after this will do bad things */
|
|
|
|
int width, height;
|
|
|
|
divine_size(tty, &width, &height);
|
|
|
|
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "Identified size: %d x %d\n", width, height);
|
2014-03-24 07:18:11 +04:00
|
|
|
|
|
|
|
size.ws_row = height;
|
|
|
|
size.ws_col = width;
|
|
|
|
|
|
|
|
ioctl_fs(tty, TIOCSWINSZ, &size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-31 09:27:45 +04:00
|
|
|
static int shell_fix_mouse(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
|
|
|
|
fs_node_t * mouse = kopen("/dev/mouse", 0);
|
|
|
|
if (mouse) {
|
|
|
|
ioctl_fs(mouse, 1, NULL);
|
|
|
|
close_fs(mouse);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-10 07:13:11 +04:00
|
|
|
static int shell_mount(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
if (argc < 4) {
|
|
|
|
fprintf(tty, "Usage: %s type device mountpoint\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -vfs_mount_type(argv[1], argv[2], argv[3]);
|
|
|
|
}
|
|
|
|
|
2014-04-12 09:43:39 +04:00
|
|
|
static int shell_exit(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
kexit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-04 13:05:12 +03:00
|
|
|
static int shell_cursor_off(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
outportb(0x3D4, 14);
|
|
|
|
outportb(0x3D5, 0xFF);
|
|
|
|
outportb(0x3D4, 15);
|
|
|
|
outportb(0x3D5, 0xFF);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-15 13:18:57 +03:00
|
|
|
extern pid_t trace_pid;
|
|
|
|
static int shell_debug_pid(fs_node_t * tty, int argc, char * argv[]) {
|
|
|
|
trace_pid = atoi(argv[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:29:40 +04:00
|
|
|
static struct shell_command shell_commands[] = {
|
2013-11-30 12:09:04 +04:00
|
|
|
{"shell", &shell_create_userspace_shell,
|
|
|
|
"Runs a userspace shell on this tty."},
|
2014-06-08 07:44:13 +04:00
|
|
|
{"login", &shell_replace_login,
|
|
|
|
"Replace the debug shell with /bin/login."},
|
2013-11-30 12:09:04 +04:00
|
|
|
{"echo", &shell_echo,
|
|
|
|
"Prints arguments."},
|
|
|
|
{"help", &shell_help,
|
|
|
|
"Prints a list of possible shell commands and their descriptions."},
|
2013-12-01 10:27:45 +04:00
|
|
|
{"cd", &shell_cd,
|
|
|
|
"Change current directory."},
|
|
|
|
{"ls", &shell_ls,
|
|
|
|
"List files in current or other directory."},
|
2013-12-15 01:47:18 +04:00
|
|
|
{"log", &shell_log,
|
|
|
|
"Configure serial debug logging."},
|
2013-12-13 11:40:52 +04:00
|
|
|
{"pci", &shell_pci,
|
2013-12-15 01:47:18 +04:00
|
|
|
"Print PCI devices, as well as their names and BARs."},
|
|
|
|
{"uid", &shell_uid,
|
2014-04-12 09:51:16 +04:00
|
|
|
"Change the effective user id of the shell."},
|
2014-03-10 06:36:28 +04:00
|
|
|
{"mod", &shell_mod,
|
|
|
|
"[testing] Module loading."},
|
|
|
|
{"symbols", &shell_symbols,
|
|
|
|
"Dump symbol table."},
|
2016-09-15 13:18:57 +03:00
|
|
|
{"debug_pid", &shell_debug_pid,
|
|
|
|
"Set pid to trace syscalls for."},
|
2014-03-10 06:36:28 +04:00
|
|
|
{"print", &shell_print,
|
|
|
|
"[dangerous] Print the value of a symbol using a format string."},
|
2014-03-15 11:03:48 +04:00
|
|
|
{"modules", &shell_modules,
|
|
|
|
"Print names and addresses of all loaded modules."},
|
2014-03-24 07:18:11 +04:00
|
|
|
{"divine-size", &shell_divinesize,
|
2014-04-12 09:50:02 +04:00
|
|
|
"Attempt to discover TTY size of serial."},
|
2014-05-31 09:27:45 +04:00
|
|
|
{"fix-mouse", &shell_fix_mouse,
|
|
|
|
"Attempt to reset mouse device."},
|
2014-06-10 07:13:11 +04:00
|
|
|
{"mount", &shell_mount,
|
|
|
|
"Mount a filesystemp."},
|
2015-04-15 09:05:07 +03:00
|
|
|
{"rdtsc", &shell_rdtsc,
|
|
|
|
"Read the TSC, if available."},
|
|
|
|
{"mhz", &shell_mhz,
|
|
|
|
"Use TSC to determine clock speed."},
|
2016-09-04 13:05:12 +03:00
|
|
|
{"cursor-off", &shell_cursor_off,
|
|
|
|
"Disable VGA text mode cursor."},
|
2014-04-12 09:43:39 +04:00
|
|
|
{"exit", &shell_exit,
|
|
|
|
"Quit the shell."},
|
2013-11-30 12:09:04 +04:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2014-03-15 10:37:42 +04:00
|
|
|
void debug_shell_install(struct shell_command * sh) {
|
|
|
|
hashmap_set(shell_commands_map, sh->name, sh);
|
|
|
|
}
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/*
|
|
|
|
* A TTY object to pass to the tasklets for handling
|
|
|
|
* serial-tty interaction. This probably shouldn't
|
|
|
|
* be done as tasklets - TTYs should just be able
|
|
|
|
* to wrap existing fs_nodes themselves, but that's
|
|
|
|
* a problem for another day.
|
|
|
|
*/
|
2013-11-28 10:24:58 +04:00
|
|
|
struct tty_o {
|
|
|
|
fs_node_t * node;
|
|
|
|
fs_node_t * tty;
|
|
|
|
};
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/*
|
|
|
|
* These tasklets handle tty-serial interaction.
|
|
|
|
*/
|
2014-03-15 10:17:59 +04:00
|
|
|
static void debug_shell_handle_in(void * data, char * name) {
|
2013-11-28 10:24:58 +04:00
|
|
|
struct tty_o * tty = (struct tty_o *)data;
|
2014-05-28 08:38:30 +04:00
|
|
|
|
2013-11-28 10:24:58 +04:00
|
|
|
while (1) {
|
|
|
|
uint8_t buf[1];
|
|
|
|
int r = read_fs(tty->tty, 0, 1, (unsigned char *)buf);
|
|
|
|
write_fs(tty->node, 0, r, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 10:17:59 +04:00
|
|
|
static void debug_shell_handle_out(void * data, char * name) {
|
2013-11-28 10:24:58 +04:00
|
|
|
struct tty_o * tty = (struct tty_o *)data;
|
2014-05-28 08:38:30 +04:00
|
|
|
|
2013-11-28 10:24:58 +04:00
|
|
|
while (1) {
|
|
|
|
uint8_t buf[1];
|
|
|
|
int r = read_fs(tty->node, 0, 1, (unsigned char *)buf);
|
|
|
|
write_fs(tty->tty, 0, r, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 09:43:39 +04:00
|
|
|
static void debug_shell_actual(void * data, char * name) {
|
|
|
|
|
2015-06-03 05:09:48 +03:00
|
|
|
current_process->image.entry = 0;
|
2014-04-12 09:43:39 +04:00
|
|
|
fs_node_t * tty = (fs_node_t *)data;
|
2013-11-30 12:09:04 +04:00
|
|
|
|
|
|
|
/* Our prompt will include the version number of the current kernel */
|
2013-11-28 07:11:58 +04:00
|
|
|
char version_number[1024];
|
|
|
|
sprintf(version_number, __kernel_version_format,
|
|
|
|
__kernel_version_major,
|
|
|
|
__kernel_version_minor,
|
|
|
|
__kernel_version_lower,
|
|
|
|
__kernel_version_suffix);
|
2013-11-28 10:24:58 +04:00
|
|
|
|
2013-12-02 11:29:40 +04:00
|
|
|
/* Initialize the shell commands map */
|
2013-11-30 12:09:04 +04:00
|
|
|
int retval = 0;
|
|
|
|
|
2013-11-28 07:11:58 +04:00
|
|
|
while (1) {
|
|
|
|
char command[512];
|
|
|
|
|
2013-11-28 09:21:39 +04:00
|
|
|
/* Print out the prompt */
|
2013-11-30 12:09:04 +04:00
|
|
|
if (retval) {
|
2014-04-06 03:12:09 +04:00
|
|
|
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);
|
2013-11-30 12:09:04 +04:00
|
|
|
} else {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "\033[1;34m%s-%s %s#\033[0m ", __kernel_name, version_number, current_process->wd_name);
|
2013-11-30 12:09:04 +04:00
|
|
|
}
|
2013-11-28 09:21:39 +04:00
|
|
|
|
|
|
|
/* Read a line */
|
2013-11-28 10:24:58 +04:00
|
|
|
debug_shell_readline(tty, command, 511);
|
2013-11-28 09:21:39 +04:00
|
|
|
|
2013-11-28 10:41:27 +04:00
|
|
|
char * arg = strdup(command);
|
|
|
|
char * argv[1024]; /* Command tokens (space-separated elements) */
|
2013-12-02 07:37:13 +04:00
|
|
|
int argc = tokenize(arg, " ", argv);
|
2013-11-28 10:41:27 +04:00
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
if (!argc) continue;
|
|
|
|
|
2013-11-30 12:09:04 +04:00
|
|
|
/* Parse the command string */
|
2013-12-02 11:29:40 +04:00
|
|
|
struct shell_command * sh = hashmap_get(shell_commands_map, argv[0]);
|
|
|
|
if (sh) {
|
|
|
|
retval = sh->function(tty, argc, argv);
|
|
|
|
} else {
|
2014-04-06 03:12:09 +04:00
|
|
|
fprintf(tty, "Unrecognized command: %s\n", argv[0]);
|
2013-11-28 09:21:39 +04:00
|
|
|
}
|
2013-11-28 10:41:27 +04:00
|
|
|
|
|
|
|
free(arg);
|
2013-11-28 07:11:58 +04:00
|
|
|
}
|
2014-04-12 09:43:39 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tasklet for managing the kernel serial console.
|
|
|
|
* This is basically a very simple shell, with access
|
|
|
|
* to some internal kernel commands, and (eventually)
|
|
|
|
* debugging routines.
|
|
|
|
*/
|
|
|
|
static void debug_shell_run(void * data, char * name) {
|
|
|
|
/*
|
|
|
|
* We will run on the first serial port.
|
|
|
|
* TODO detect that this failed
|
|
|
|
*/
|
|
|
|
fs_node_t * tty = kopen("/dev/ttyS0", 0);
|
|
|
|
|
2014-05-03 11:15:07 +04:00
|
|
|
fs_node_t * fs_master;
|
|
|
|
fs_node_t * fs_slave;
|
2014-04-12 09:43:39 +04:00
|
|
|
|
2014-05-03 11:15:07 +04:00
|
|
|
pty_create(NULL, &fs_master, &fs_slave);
|
2014-04-12 09:43:39 +04:00
|
|
|
|
|
|
|
/* Attach the serial to the TTY interface */
|
2014-05-03 11:15:07 +04:00
|
|
|
struct tty_o _tty = {.node = fs_master, .tty = tty};
|
2014-04-12 09:43:39 +04:00
|
|
|
|
|
|
|
create_kernel_tasklet(debug_shell_handle_in, "[kttydebug-in]", (void *)&_tty);
|
|
|
|
create_kernel_tasklet(debug_shell_handle_out, "[kttydebug-out]", (void *)&_tty);
|
|
|
|
|
|
|
|
/* Set the device to be the actual TTY slave */
|
2014-05-03 11:15:07 +04:00
|
|
|
tty = fs_slave;
|
2014-04-12 09:43:39 +04:00
|
|
|
|
2014-05-28 08:38:30 +04:00
|
|
|
fs_master->refcount = -1;
|
|
|
|
fs_slave->refcount = -1;
|
|
|
|
|
2014-04-12 09:43:39 +04:00
|
|
|
current_process->fds->entries[0] = tty;
|
|
|
|
current_process->fds->entries[1] = tty;
|
|
|
|
current_process->fds->entries[2] = tty;
|
2014-05-09 07:24:26 +04:00
|
|
|
current_process->fds->length = 3;
|
2014-04-12 09:43:39 +04:00
|
|
|
|
2014-05-30 09:01:53 +04:00
|
|
|
tty_set_vintr(tty, 0x02);
|
|
|
|
|
|
|
|
fprintf(tty, "\n\n"
|
|
|
|
"Serial debug console started.\n"
|
|
|
|
"Type `help` for a list of commands.\n"
|
|
|
|
"To access a userspace shell, type `shell`.\n"
|
|
|
|
"Use ^B to send SIGINT instead of ^C.\n"
|
|
|
|
"\n");
|
|
|
|
|
2014-04-12 09:43:39 +04:00
|
|
|
debug_shell_actual(tty, name);
|
2013-11-28 07:11:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int debug_shell_start(void) {
|
2014-04-11 10:08:07 +04:00
|
|
|
/* Setup shell commands */
|
|
|
|
shell_commands_map = hashmap_create(10);
|
|
|
|
struct shell_command * sh = &shell_commands[0];
|
|
|
|
while (sh->name) {
|
|
|
|
hashmap_set(shell_commands_map, sh->name, sh);
|
|
|
|
sh++;
|
|
|
|
}
|
|
|
|
|
2014-04-12 09:43:39 +04:00
|
|
|
debug_hook = debug_shell_actual;
|
|
|
|
|
2013-11-28 09:21:39 +04:00
|
|
|
int i = create_kernel_tasklet(debug_shell_run, "[kttydebug]", NULL);
|
2013-11-28 07:11:58 +04:00
|
|
|
debug_print(NOTICE, "Started tasklet with pid=%d", i);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-15 10:17:59 +04:00
|
|
|
|
|
|
|
int debug_shell_stop(void) {
|
|
|
|
debug_print(NOTICE, "Tried to unload debug shell, but debug shell has no real shutdown routine. Don't do that!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DEF(debugshell, debug_shell_start, debug_shell_stop);
|
2014-03-19 07:11:56 +04:00
|
|
|
MODULE_DEPENDS(serial);
|