toaruos/modules/debug_shell.c

945 lines
25 KiB
C
Raw Normal View History

/*
* Kernel Debug Shell
*/
#include <system.h>
#include <fs.h>
#include <logging.h>
#include <process.h>
#include <version.h>
2013-11-28 10:24:58 +04:00
#include <termios.h>
#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>
#include <ipv4.h>
#include <elf.h>
#include <module.h>
2013-11-28 10:24:58 +04:00
#include <mod/shell.h>
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-03-15 10:17:59 +04:00
static void 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-03-15 10:17:59 +04:00
static void set_buffered(fs_node_t * dev) {
2013-11-28 10:24:58 +04:00
ioctl_fs(dev, TCSETSF, &old);
}
2013-11-30 12:09:04 +04:00
/*
* TODO move this to the printf module
*/
void fs_printf(fs_node_t * device, char *fmt, ...) {
va_list args;
va_start(args, fmt);
char buffer[1024];
vasprintf(buffer, fmt, args);
va_end(args);
write_fs(device, 0, strlen(buffer), (uint8_t *)buffer);
}
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-03-15 10:17:59 +04:00
static int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
int read = 0;
2013-11-28 10:24:58 +04:00
set_unbuffered(dev);
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];
if (buf[0] == '\n') {
fs_printf(dev, "\n");
linebuf[read] = 0;
break;
} else if (buf[0] == 0x08) {
if (read > 0) {
fs_printf(dev, "\010 \010");
read--;
linebuf[read] = 0;
}
continue;
}
fs_printf(dev, "%c", buf[0]);
read += r;
}
2013-11-28 10:24:58 +04:00
set_buffered(dev);
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) {
char * argv[] = {
"/bin/sh",
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[]) {
2013-12-13 11:40:52 +04:00
int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", NULL);
2013-11-30 12:09:04 +04:00
fs_printf(tty, "Shell started with pid = %d\n", pid);
process_t * child_task = process_from_pid(pid);
sleep_on(child_task->wait_queue);
return child_task->status;
}
static int shell_echo(fs_node_t * tty, int argc, char * argv[]) {
for (int i = 1; i < argc; ++i) {
fs_printf(tty, "%s ", argv[i]);
}
fs_printf(tty, "\n");
return 0;
}
static int shell_help(fs_node_t * tty, int argc, char * argv[]) {
list_t * hash_keys = hashmap_keys(shell_commands_map);
foreach(_key, hash_keys) {
char * key = (char *)_key->value;
struct shell_command * c = hashmap_get(shell_commands_map, key);
fs_printf(tty, "%s - %s\n", c->name, c->description);
2013-11-30 12:09:04 +04:00
}
list_free(hash_keys);
free(hash_keys);
2013-11-30 12:09:04 +04:00
return 0;
}
static int shell_cd(fs_node_t * tty, int argc, char * argv[]) {
if (argc < 2) {
return -1;
}
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) {
return -1;
}
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 {
return -1;
}
}
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) {
fs_printf(tty, "%s\n", kentry->name);
index++;
kentry = readdir_fs(wd, index);
}
close_fs(wd);
free(wd);
return 0;
}
static int shell_test_hash(fs_node_t * tty, int argc, char * argv[]) {
fs_printf(tty, "Creating a hash...\n");
hashmap_t * map = hashmap_create(2);
hashmap_set(map, "a", (void *)1);
hashmap_set(map, "b", (void *)2);
hashmap_set(map, "c", (void *)3);
fs_printf(tty, "value at a: %d\n", (int)hashmap_get(map, "a"));
fs_printf(tty, "value at b: %d\n", (int)hashmap_get(map, "b"));
fs_printf(tty, "value at c: %d\n", (int)hashmap_get(map, "c"));
hashmap_set(map, "b", (void *)42);
fs_printf(tty, "value at a: %d\n", (int)hashmap_get(map, "a"));
fs_printf(tty, "value at b: %d\n", (int)hashmap_get(map, "b"));
fs_printf(tty, "value at c: %d\n", (int)hashmap_get(map, "c"));
hashmap_remove(map, "a");
fs_printf(tty, "value at a: %d\n", (int)hashmap_get(map, "a"));
fs_printf(tty, "value at b: %d\n", (int)hashmap_get(map, "b"));
fs_printf(tty, "value at c: %d\n", (int)hashmap_get(map, "c"));
fs_printf(tty, "map contains a: %s\n", hashmap_has(map, "a") ? "yes" : "no");
fs_printf(tty, "map contains b: %s\n", hashmap_has(map, "b") ? "yes" : "no");
fs_printf(tty, "map contains c: %s\n", hashmap_has(map, "c") ? "yes" : "no");
list_t * hash_keys = hashmap_keys(map);
foreach(_key, hash_keys) {
char * key = (char *)_key->value;
fs_printf(tty, "map[%s] = %d\n", key, (int)hashmap_get(map, key));
}
list_free(hash_keys);
free(hash_keys);
hashmap_free(map);
free(map);
return 0;
}
2013-12-15 01:47:18 +04:00
static int shell_log(fs_node_t * tty, int argc, char * argv[]) {
if (argc < 2) {
2013-12-15 01:47:18 +04:00
fs_printf(tty, "Log level is currently %d.\n", debug_level);
2014-03-16 06:48:51 +04:00
fs_printf(tty, "Serial logging is %s.\n", !!kprint_to_file ? "enabled" : "disabled");
2013-12-15 01:47:18 +04:00
fs_printf(tty, "Usage: log [on|off] [<level>]\n");
} else {
2013-12-15 01:47:18 +04:00
if (!strcmp(argv[1], "on")) {
2014-03-16 06:48:51 +04:00
kprint_to_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-03-16 06:48:51 +04:00
kprint_to_file = NULL;
2013-12-15 01:47:18 +04:00
}
}
return 0;
}
2013-12-02 11:50:13 +04:00
static void dumb_sort(char * str) {
int size = strlen(str);
for (int i = 0; i < size-1; ++i) {
for (int j = 0; j < size-1; ++j) {
if (str[j] > str[j+1]) {
char t = str[j+1];
str[j+1] = str[j];
str[j] = t;
}
}
}
}
static int shell_anagrams(fs_node_t * tty, int argc, char * argv[]) {
hashmap_t * map = hashmap_create(10);
for (int i = 1; i < argc; ++i) {
char * c = strdup(argv[i]);
dumb_sort(c);
list_t * l = hashmap_get(map, c);
if (!l) {
l = list_create();
hashmap_set(map, c, l);
}
list_insert(l, argv[i]);
free(c);
}
list_t * values = hashmap_values(map);
foreach(val, values) {
list_t * x = (list_t *)val->value;
fs_printf(tty, "{");
foreach(node, x) {
fs_printf(tty, "%s", (char *)node->value);
if (node->next) {
fs_printf(tty, ", ");
}
}
fs_printf(tty, "}%s", (!!val->next) ? ", " : "\n");
free(x);
}
list_free(values);
free(values);
hashmap_free(map);
free(map);
return 0;
}
2013-12-13 11:40:52 +04:00
static void scan_hit_list(uint32_t device, uint16_t vendorid, uint16_t deviceid) {
fs_node_t * tty = current_process->fds->entries[0];
fs_printf(tty, "%x:%x.%x (%x, %x:%x) %s %s\n",
(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));
fs_printf(tty, " BAR0: 0x%x\n", pci_read_field(device, PCI_BAR0, 4));
fs_printf(tty, " BAR1: 0x%x\n", pci_read_field(device, PCI_BAR1, 4));
fs_printf(tty, " BAR2: 0x%x\n", pci_read_field(device, PCI_BAR2, 4));
fs_printf(tty, " BAR3: 0x%x\n", pci_read_field(device, PCI_BAR3, 4));
fs_printf(tty, " BAR4: 0x%x\n", pci_read_field(device, PCI_BAR4, 4));
fs_printf(tty, " BAR6: 0x%x\n", pci_read_field(device, PCI_BAR5, 4));
}
static int shell_pci(fs_node_t * tty, int argc, char * argv[]) {
pci_scan(&scan_hit_list, -1);
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) {
fs_printf(tty, "uid=%d\n", current_process->user);
} else {
current_process->user = atoi(argv[1]);
}
return 0;
}
static uint32_t rtl_device_pci = 0x00000000;
static void find_rtl(uint32_t device, uint16_t vendorid, uint16_t deviceid) {
if ((vendorid == 0x10ec) && (deviceid == 0x8139)) {
rtl_device_pci = device;
}
}
#define RTL_PORT_MAC 0x00
#define RTL_PORT_MAR 0x08
#define RTL_PORT_RBSTART 0x30
#define RTL_PORT_CMD 0x37
#define RTL_PORT_IMR 0x3C
#define RTL_PORT_ISR 0x3E
#define RTL_PORT_RCR 0x44
#define RTL_PORT_CONFIG 0x52
static uint8_t rtl_rx_buffer[8192+16];
static int shell_rtl(fs_node_t * tty, int argc, char * argv[]) {
pci_scan(&find_rtl, -1);
if (rtl_device_pci) {
fs_printf(tty, "Located an RTL 8139: 0x%x\n", rtl_device_pci);
uint16_t command_reg = pci_read_field(rtl_device_pci, PCI_COMMAND, 2);
fs_printf(tty, "COMMAND register before: 0x%4x\n", command_reg);
if (command_reg & 0x0002) {
fs_printf(tty, "Bus mastering already enabled.\n");
} else {
command_reg |= 0x2; /* bit 2 */
fs_printf(tty, "COMMAND register after: 0x%4x\n", command_reg);
fs_printf(tty, "XXX: I can't write config registers :(\n");
return -1;
}
uint32_t rtl_irq = pci_read_field(rtl_device_pci, PCI_INTERRUPT_LINE, 1);
fs_printf(tty, "Interrupt Line: %x\n", rtl_irq);
uint32_t rtl_bar0 = pci_read_field(rtl_device_pci, PCI_BAR0, 4);
uint32_t rtl_bar1 = pci_read_field(rtl_device_pci, PCI_BAR1, 4);
fs_printf(tty, "BAR0: 0x%8x\n", rtl_bar0);
fs_printf(tty, "BAR1: 0x%8x\n", rtl_bar1);
uint32_t rtl_iobase = 0x00000000;
if (rtl_bar0 & 0x00000001) {
rtl_iobase = rtl_bar0 & 0xFFFFFFFC;
} else {
fs_printf(tty, "This doesn't seem right! RTL8139 should be using an I/O BAR; this looks like a memory bar.");
}
fs_printf(tty, "RTL iobase: 0x%x\n", rtl_iobase);
fs_printf(tty, "Determining mac address...\n");
uint8_t mac[6];
for (int i = 0; i < 6; ++i) {
mac[i] = inports(rtl_iobase + RTL_PORT_MAC + i);
}
fs_printf(tty, "%2x:%2x:%2x:%2x:%2x:%2x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
fs_printf(tty, "Enabling RTL8139.\n");
outportb(rtl_iobase + RTL_PORT_CONFIG, 0x0);
fs_printf(tty, "Resetting RTL8139.\n");
outportb(rtl_iobase + RTL_PORT_CMD, 0x10);
while ((inportb(rtl_iobase + 0x37) & 0x10) != 0) { }
fs_printf(tty, "Done resetting RTL8139.\n");
fs_printf(tty, "Initializing receive buffer.\n");
outportl(rtl_iobase + RTL_PORT_RBSTART, (unsigned long)&rtl_rx_buffer);
fs_printf(tty, "Enabling IRQs.\n");
outports(rtl_iobase + RTL_PORT_IMR, 0x0005); /* TOK, ROK */
fs_printf(tty, "Configuring receive buffer.\n");
outportl(rtl_iobase + RTL_PORT_RCR, 0xF | (1 << 7)); /* 0xF = AB+AM+APM+AAP */
fs_printf(tty, "Enabling receive and transmit.\n");
outportb(rtl_iobase + RTL_PORT_CMD, 0x0C);
#if 0
fs_printf(tty, "Going to try to force-send a UDP packet...\n");
struct ipv4_packet p;
p.version_ihl = (4 << 4) & (5 << 0); /* IPv4, no options */
p.dscp_ecn = 0; /* nope nope nope */
p.length = sizeof(struct ipv4_packet) + sizeof(struct udp_packet) + sizeof(struct dhcp_packet);
p.ident = 0;
p.flags_fragment = 0;
p.ttl = 0xFF;
p.protocol = 17;
p.checksum = 0; /* calculate this later */
p.source = 0x00000000; /* 0.0.0.0 */
p.destination = 0xFFFFFFFF; /* 255.255.255.255 */
uint16_t * packet = (uint16_t *)&p;
uint32_t total = 0;
for (int i = 0; i < 10; ++i) {
total += packet[i];
if (total & 0x80000000) {
total = (total & 0xFFFF) + (total >> 16);
}
}
while (total >> 16) {
total = (total & 0xFFFF) + (total >> 16);
}
p.checksum = ~total;
struct udp_packet u;
u.source = p.source;
u.destination = p.destination;
u.zeroes = 0;
u.protocol = p.protocol;
u.udp_length = p.length;
u.source_port = 68;
u.destination_port = 67;
u.length = sizeof(struct dhcp_packet);
u.checksum = 0;
#endif
} else {
return -1;
}
return 0;
}
2014-01-10 12:45:59 +04:00
typedef struct packet {
fs_node_t * client_port; /* client "port"... it's actually the pointer to the pipe for the client. */
pid_t client_pid; /* the pid of the client is always include because reasons */
size_t size; /* size of the packet */
uint8_t data[];
} packet_t;
static void packet_send(fs_node_t * recver, fs_node_t * sender, size_t size, void * data) {
size_t p_size = size + sizeof(struct packet);
packet_t * p = malloc(p_size);
memcpy(p->data, data, size);
p->client_port = sender;
p->client_pid = current_process->id;
p->size = size;
write_fs(recver, 0, p_size, (uint8_t *)p);
free(p);
}
static void packet_recv(fs_node_t * socket, packet_t ** out) {
packet_t tmp;
read_fs(socket, 0, sizeof(struct packet), (uint8_t *)&tmp);
*out = malloc(tmp.size + sizeof(struct packet));
memcpy(*out, &tmp, sizeof(struct packet));
read_fs(socket, 0, tmp.size, (uint8_t *)(*out)->data);
}
static void tasklet_client(void * data, char * name) {
fs_node_t * server_pipe = (fs_node_t *)data;
fs_node_t * client_pipe = make_pipe(4096);
fs_node_t * tty = current_process->fds->entries[0];
packet_send(server_pipe, client_pipe, strlen("Hello")+1, "Hello");
while (1) {
packet_t * p;
packet_recv(client_pipe, &p);
fs_printf(tty, "Client %s Received: %s\n", name, (char *)p->data);
if (!strcmp((char*)p->data, "PING")) {
packet_send(server_pipe, client_pipe, strlen("PONG")+1, "PONG");
}
free(p);
}
}
2014-01-15 09:21:33 +04:00
static int shell_server_running = 0;
static fs_node_t * shell_server_node = NULL;
static void tasklet_server(void * data, char * name) {
fs_node_t * tty = current_process->fds->entries[0];
2014-01-10 12:45:59 +04:00
fs_node_t * socket = make_pipe(4096);
2014-01-15 09:21:33 +04:00
shell_server_node = socket;
2014-01-10 12:45:59 +04:00
create_kernel_tasklet(tasklet_client, "ktty-client-1", socket);
create_kernel_tasklet(tasklet_client, "ktty-client-2", socket);
create_kernel_tasklet(tasklet_client, "ktty-client-3", socket);
2014-01-15 09:21:33 +04:00
fs_printf(tty, "Going to perform a quick demo...\n");
2014-01-10 12:45:59 +04:00
int i = 0;
fs_node_t * outputs[3];
while (i < 3) {
packet_t * p;
packet_recv(socket, &p);
fs_printf(tty, "Server received %s from %d:%d\n", (char*)p->data, p->client_pid, p->client_port);
packet_send(p->client_port, socket, strlen("Welcome!")+1, "Welcome!");
outputs[i] = p->client_port;
free(p);
i++;
}
fs_printf(tty, "Okay, that's everyone, time to send some responses.\n");
i = 0;
while (i < 3) {
packet_send(outputs[i], socket, strlen("PING")+1, "PING");
i++;
}
i = 0;
while (i < 3) {
packet_t * p;
packet_recv(socket, &p);
fs_printf(tty, "PONG from %d\n", p->client_pid);
free(p);
i++;
}
fs_printf(tty, "And that's the demo of packet servers.\n");
2014-01-15 09:21:33 +04:00
fs_printf(tty, "Now running in echo mode, will respond to all clients with whatever they sent.\n");
while (1) {
packet_t * p;
packet_recv(socket, &p);
packet_send(p->client_port, socket, p->size, p->data);
free(p);
}
}
static int shell_server_test(fs_node_t * tty, int argc, char * argv[]) {
if (!shell_server_running) {
shell_server_running = 1;
create_kernel_tasklet(tasklet_server, "ktty-server", NULL);
fs_printf(tty, "Started server.\n");
}
return 0;
}
static int shell_client_test(fs_node_t * tty, int argc, char * argv[]) {
if (!shell_server_running) {
fs_printf(tty, "No server running, won't be able to connect.\n");
return 1;
}
if (argc < 2) {
fs_printf(tty, "expected argument\n");
return 1;
}
fs_node_t * client_pipe = make_pipe(4096);
packet_send(shell_server_node, client_pipe, strlen(argv[1])+1, argv[1]);
while (1) {
packet_t * p;
packet_recv(client_pipe, &p);
fs_printf(tty, "Got response from server: %s\n", (char *)p->data);
free(p);
break;
}
close_fs(client_pipe);
2014-01-10 12:45:59 +04:00
return 0;
}
2014-03-10 09:37:49 +04:00
char * special_thing = "I am a string from the kernel.\n";
static int shell_mod(fs_node_t * tty, int argc, char * argv[]) {
if (argc < 2) {
fs_printf(tty, "expected argument\n");
return 1;
}
fs_node_t * file = kopen(argv[1], 0);
if (!file) {
fs_printf(tty, "Failed to load module: %s\n", argv[1]);
return 1;
}
fs_printf(tty, "Okay, going to load a module!\n");
module_data_t * mod_info = module_load(argv[1]);
2014-03-12 11:18:55 +04:00
if (!mod_info) {
fs_printf(tty, "Something went wrong, failed to load module: %s\n", argv[1]);
return 1;
}
fs_printf(tty, "Loaded %s at 0x%x\n", mod_info->mod_info->name, mod_info->bin_data);
2014-03-10 09:37:49 +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) {
fs_printf(tty, "0x%x - %s\n", k->addr, k->name);
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) {
fs_printf(tty, "print format_string symbol_name\n");
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) {
fs_printf(tty, format, k->addr);
} else {
fs_printf(tty, format, *((uintptr_t *)k->addr));
}
fs_printf(tty, "\n");
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;
module_data_t * mod_info = hashmap_get(modules_get_list(), key);
2014-03-15 11:03:48 +04:00
fs_printf(tty, "%s at 0x%x {.init=0x%x, .fini=0x%x}\n",
mod_info->mod_info->name, mod_info->bin_data,
mod_info->mod_info->initialize, mod_info->mod_info->finalize);
2014-03-15 11:03:48 +04:00
}
return 0;
}
2014-03-15 11:50:17 +04:00
static int shell_mem_info(fs_node_t * tty, int argc, char * argv[]) {
unsigned int total = memory_total();
unsigned int free = total - memory_use();
extern uintptr_t heap_end;
fs_printf(tty, "Total: %d kB\n", total);
fs_printf(tty, "Free: %d kB\n", free);
fs_printf(tty, "Heap End: 0x%x\n", heap_end);
return 0;
}
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."},
{"echo", &shell_echo,
"Prints arguments."},
{"help", &shell_help,
"Prints a list of possible shell commands and their descriptions."},
{"cd", &shell_cd,
"Change current directory."},
{"ls", &shell_ls,
"List files in current or other directory."},
{"test-hash", &shell_test_hash,
"Test hashmap functionality."},
2013-12-15 01:47:18 +04:00
{"log", &shell_log,
"Configure serial debug logging."},
2013-12-02 11:50:13 +04:00
{"anagrams", &shell_anagrams,
"Demo of hashmaps and lists. Give a list of words, get a grouping of anagrams."},
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,
"Change the effective user id of the shell (useful when running `shell`)."},
2014-01-10 12:45:59 +04:00
{"server-test", &shell_server_test,
"Spawn a packet server and some clients."},
2014-01-15 09:21:33 +04:00
{"client-test", &shell_client_test,
"Communicate with packet server."},
{"rtl", &shell_rtl,
"[debug] rtl8139 initialization."},
{"mod", &shell_mod,
"[testing] Module loading."},
{"symbols", &shell_symbols,
"Dump symbol table."},
{"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-16 10:56:10 +04:00
{"meminfo", &shell_mem_info,
2014-03-15 11:50:17 +04:00
"Display various pieces of information kernel and system memory."},
2013-11-30 12:09:04 +04:00
{NULL, NULL, NULL}
};
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;
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;
while (1) {
uint8_t buf[1];
int r = read_fs(tty->node, 0, 1, (unsigned char *)buf);
write_fs(tty->tty, 0, r, buf);
}
}
2013-11-30 12:09:04 +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)
*/
2014-03-15 10:17:59 +04:00
static void divine_size(fs_node_t * dev, int * width, int * height) {
2013-11-28 10:24:58 +04:00
char tmp[100];
int read = 0;
unsigned long start_tick = timer_ticks;
2013-11-30 12:09:04 +04:00
/* Move cursor, Request position, Reset cursor */
2013-11-29 03:58:57 +04:00
fs_printf(dev, "\033[1000;1000H\033[6n\033[H");
2013-11-28 10:24:58 +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) {
2013-11-30 12:09:04 +04:00
/*
* We've timed out. This will only be triggered
* when we eventually receive something, though
*/
2013-11-28 10:24:58 +04:00
*width = 80;
*height = 23;
2013-11-30 12:09:04 +04:00
/* Clear and return */
2013-11-29 03:58:57 +04:00
fs_printf(dev, "\033[J");
2013-11-28 10:24:58 +04:00
return;
}
}
2013-11-30 12:09:04 +04:00
/* Clear */
2013-11-29 03:58:57 +04:00
fs_printf(dev, "\033[J");
2013-11-30 12:09:04 +04:00
/* Break up the result into two strings */
2013-11-28 10:24:58 +04:00
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);
2013-11-30 12:09:04 +04:00
/* And then parse it into numbers */
2013-11-28 10:24:58 +04:00
*height = atoi(tmp);
*width = atoi(h);
}
2013-11-30 12:09:04 +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.
*/
2014-03-15 10:17:59 +04:00
static void debug_shell_run(void * data, char * name) {
2013-11-30 12:09:04 +04:00
/*
* We will run on the first serial port.
* TODO detect that this failed
*/
fs_node_t * tty = kopen("/dev/ttyS0", 0);
2013-11-30 12:09:04 +04:00
/* Our prompt will include the version number of the current kernel */
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-11-30 12:09:04 +04:00
/* We will convert the serial interface into an actual TTY */
2013-11-28 10:24:58 +04:00
int master, slave;
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);
size.ws_row = height;
size.ws_col = width;
2013-11-30 12:09:04 +04:00
/* Convert the serial line into a TTY */
2013-11-28 10:24:58 +04:00
openpty(&master, &slave, NULL, NULL, &size);
2013-11-30 12:09:04 +04:00
/* Attach the serial to the TTY interface */
2013-11-28 10:24:58 +04:00
struct tty_o _tty = {.node = current_process->fds->entries[master], .tty = tty};
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 */
tty = current_process->fds->entries[slave];
2013-12-13 11:40:52 +04:00
current_process->fds->entries[0] = tty;
current_process->fds->entries[1] = tty;
current_process->fds->entries[2] = tty;
/* Initialize the shell commands map */
if (!shell_commands_map) {
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++;
}
}
2013-11-30 12:09:04 +04:00
int retval = 0;
while (1) {
char command[512];
/* Print out the prompt */
2013-11-30 12:09:04 +04:00
if (retval) {
fs_printf(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 {
fs_printf(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
}
/* Read a line */
2013-11-28 10:24:58 +04:00
debug_shell_readline(tty, command, 511);
2013-11-28 10:41:27 +04:00
char * arg = strdup(command);
char * argv[1024]; /* Command tokens (space-separated elements) */
int argc = tokenize(arg, " ", argv);
2013-11-28 10:41:27 +04:00
if (!argc) continue;
2013-11-30 12:09:04 +04:00
/* Parse the command string */
struct shell_command * sh = hashmap_get(shell_commands_map, argv[0]);
if (sh) {
retval = sh->function(tty, argc, argv);
} else {
2013-11-28 10:47:05 +04:00
fs_printf(tty, "Unrecognized command: %s\n", argv[0]);
}
2013-11-28 10:41:27 +04:00
free(arg);
}
}
int debug_shell_start(void) {
int i = create_kernel_tasklet(debug_shell_run, "[kttydebug]", NULL);
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);