diff options
author | Marvin Borner | 2020-11-14 22:27:59 +0100 |
---|---|---|
committer | Marvin Borner | 2020-11-14 22:27:59 +0100 |
commit | 0621c44e439bedf034399e1bff139ef506039eba (patch) | |
tree | 68e69323391e5c4725fa6ef2d16e17e6664011b4 | |
parent | 1af60d64fb97cfadab39b9d3e86248473cac0693 (diff) |
Some fixes
I WAS BUG-SEARCHING FOR SEVERAL HOURS
WHYYYY, WHEEERE, AAAAAH
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | kernel/drivers/interrupts.c | 3 | ||||
-rw-r--r-- | kernel/drivers/keyboard.c | 40 | ||||
-rw-r--r-- | kernel/drivers/mouse.c | 25 | ||||
-rw-r--r-- | kernel/drivers/rtl8139.c | 4 | ||||
-rw-r--r-- | kernel/features/event.c | 2 | ||||
-rw-r--r-- | kernel/features/net.c | 67 | ||||
-rw-r--r-- | kernel/inc/interrupts.h | 3 | ||||
-rw-r--r-- | kernel/inc/net.h | 3 | ||||
-rw-r--r-- | libc/mem.c | 50 |
10 files changed, 106 insertions, 93 deletions
@@ -1,7 +1,7 @@ # MIT License, Copyright (c) 2020 Marvin Borner # Kernel optimization -OPTIMIZATION = -Ofast +OPTIMIZATION = -Os # Remove tree optimizations for kernel #CFLAGS_EXTRA = -fno-tree-bit-ccp -fno-tree-builtin-call-dce -fno-tree-ccp -fno-tree-ch -fno-tree-coalesce-vars -fno-tree-copy-prop -fno-tree-dce -fno-tree-dominator-opts -fno-tree-dse -fno-tree-fre -fno-tree-pta -fno-tree-sink -fno-tree-slsr -fno-tree-sra -fno-tree-ter -fno-tree-loop-vectorize -fno-inline-functions -fno-inline-functions-called-once diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index b0751d6..dd10ac6 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -13,6 +13,9 @@ * IDT */ +struct idt_entry idt[256] = { 0 }; +struct idt_ptr idt_ptr = { 0 }; + void idt_set_gate(u8 num, u32 base, u16 sel, u8 flags) { // Specify the interrupt routine's base address diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c index 67e0a72..ac97d36 100644 --- a/kernel/drivers/keyboard.c +++ b/kernel/drivers/keyboard.c @@ -8,11 +8,10 @@ #include <print.h> #include <sys.h> -char keymap[128]; -struct event_keyboard *event; +static struct event_keyboard *event = NULL; -int state = 0; -int merged = 0; +static int state = 0; +static int merged = 0; void keyboard_handler() { int scancode = inb(0x60); @@ -29,7 +28,8 @@ void keyboard_handler() // TODO: "Merge" scancode to linux keycode? /* printf("%x %x = %x\n", scancode, state ? 0xe0 : 0, merged); */ - free(event); + if (event) + free(event); event = malloc(sizeof(*event)); event->magic = KEYBOARD_MAGIC; event->press = (scancode & 0x80) == 0; @@ -58,33 +58,3 @@ void keyboard_install(void) //keyboard_rate(); TODO: Fix keyboard rate? irq_install_handler(1, keyboard_handler); } - -char keymap[128] = { - 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', - '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', - '\n', 17, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', - 14, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 14, '*', - 0, // Alt key - ' ', // Space bar - 15, // Caps lock - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // F keys - 0, // Num lock - 0, // Scroll lock - 0, // Home key - 0, // Up arrow - 0, // Page up - '-', - 0, // Left arrow - 0, - 0, // Right arrow - '+', - 0, // End key - 0, // Down arrow - 0, // Page down - 0, // Insert key - 0, // Delete key - 0, 0, 0, - 0, // F11 - 0, // F12 - 0, // Other keys -}; diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c index 9b34b0a..d58fbfc 100644 --- a/kernel/drivers/mouse.c +++ b/kernel/drivers/mouse.c @@ -8,10 +8,10 @@ #include <print.h> #include <sys.h> -char mouse_cycle = 0; -char mouse_byte[3]; +static char mouse_cycle = 0; +static char mouse_byte[3] = { 0 }; -struct event_mouse *event; +static struct event_mouse *event = NULL; void mouse_handler() { @@ -30,7 +30,8 @@ void mouse_handler() case 2: mouse_byte[2] = (char)inb(0x60); - free(event); + if (event) + free(event); event = malloc(sizeof(*event)); event->magic = MOUSE_MAGIC; event->diff_x = mouse_byte[1]; @@ -140,14 +141,14 @@ void mouse_install(void) printf("4th and 5th mouse button support!\n"); /* TODO: Fix mouse laggyness - mouse_write(0xE8); - mouse_read(); - mouse_write(0x03); - mouse_read(); - mouse_write(0xF3); - mouse_read(); - mouse_write(200); - mouse_read(); */ + mouse_write(0xE8); + mouse_read(); + mouse_write(0x03); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(200); + mouse_read(); */ // Enable mouse mouse_write(0xF4); diff --git a/kernel/drivers/rtl8139.c b/kernel/drivers/rtl8139.c index b870825..fb60865 100644 --- a/kernel/drivers/rtl8139.c +++ b/kernel/drivers/rtl8139.c @@ -12,9 +12,9 @@ #include <rtl8139.h> static int rtl_irq = 0; -static u8 mac[6]; +static u8 mac[6] = { 0 }; static u8 *last_packet = NULL; -static u8 *rtl_rx_buffer; +static u8 *rtl_rx_buffer = NULL; static u32 rtl_device_pci = 0; static u32 rtl_iobase = 0; static u32 cur_rx = 0; diff --git a/kernel/features/event.c b/kernel/features/event.c index d79d531..c9bd3a4 100644 --- a/kernel/features/event.c +++ b/kernel/features/event.c @@ -8,7 +8,7 @@ #include <proc.h> #include <sys.h> -struct list *event_table[] = { [EVENT_KEYBOARD] = NULL, [EVENT_MOUSE] = NULL }; +static struct list *event_table[] = { [EVENT_KEYBOARD] = NULL, [EVENT_MOUSE] = NULL }; u32 event_register(u32 id, struct proc *proc) { diff --git a/kernel/features/net.c b/kernel/features/net.c index 75138d8..82009dd 100644 --- a/kernel/features/net.c +++ b/kernel/features/net.c @@ -70,6 +70,9 @@ static struct socket *socket_new(enum socket_type type) static int socket_close(struct socket *socket) { + if (!socket) + return 0; + struct list *list = socket_list(socket->type); if (!list) @@ -82,6 +85,9 @@ static int socket_close(struct socket *socket) return list_remove(list, iterator) != NULL; } + socket->state = S_CLOSED; + free(socket); + return 0; } @@ -279,6 +285,9 @@ static void ip_send_packet(u32 dst, void *data, int len, u8 prot) static void udp_send_packet(struct socket *socket, void *data, int len) { print("UDP send packet\n"); + if (!socket || socket->state == S_FAILED) + return; + u32 length = sizeof(struct udp_packet) + (u32)len; struct udp_packet *packet = malloc(length); memset(packet, 0, sizeof(*packet)); @@ -298,6 +307,10 @@ static void udp_send_packet(struct socket *socket, void *data, int len) static void tcp_send_packet(struct socket *socket, u16 flags, void *data, int len) { print("TCP send packet\n"); + + if (!socket || socket->state == S_FAILED) + return; + u32 length = sizeof(struct tcp_packet) + (u32)len; struct tcp_packet *packet = malloc(length); memset(packet, 0, sizeof(*packet)); @@ -426,7 +439,8 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len) printf("TCP Port: %d\n", ntohs(packet->dst_port)); struct socket *socket = NULL; - if (!(socket = socket_get(S_TCP, ntohs(packet->dst_port)))) { + if (!(socket = socket_get(S_TCP, ntohs(packet->dst_port))) || socket->state == S_CLOSED || + socket->state == S_FAILED) { print("Port isn't mapped!\n"); return; } @@ -469,6 +483,7 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len) /* tcp_send_packet(socket, TCP_FLAG_PSH | TCP_FLAG_ACK, strdup(http_res), */ /* strlen(http_res)); */ + socket->state = S_CONNECTED; tcp->state++; return; } else if (tcp->state == 3 && (flags & 0xff) == TCP_FLAG_ACK) { @@ -483,6 +498,7 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len) tcp_send_packet(socket, TCP_FLAG_FIN | TCP_FLAG_ACK, NULL, 0); + socket->state = S_CLOSED; tcp->state = 0; return; } @@ -493,11 +509,8 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len) tcp->seq_no = recv_ack; tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0); - /* tcp_send_packet(socket, TCP_FLAG_PSH | TCP_FLAG_ACK, strdup(http_req), */ - /* strlen(http_req)); */ - - /* tcp->ack_no += strlen(http_req); */ + socket->state = S_CONNECTED; tcp->state = 5; // TODO: TCP enum state machine return; } else if (tcp->state == 5 && (flags & 0xff) == TCP_FLAG_ACK) { @@ -526,6 +539,7 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len) tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0); + socket->state = S_CLOSED; tcp->state = 0; return; } @@ -546,7 +560,8 @@ static void udp_handle_packet(struct udp_packet *packet) } struct socket *socket = NULL; - if (!(socket = socket_get(S_UDP, ntohs(packet->dst_port)))) { + if (!(socket = socket_get(S_UDP, ntohs(packet->dst_port))) || socket->state == S_CLOSED || + socket->state == S_FAILED) { print("Port isn't mapped!\n"); return; } @@ -661,7 +676,8 @@ static int dhcp_discover(void) { print("DHCP discover\n"); struct socket *socket = net_open(S_UDP); - socket->src_port = DHCP_PORT; + if (socket) + socket->src_port = DHCP_PORT; if (!socket || !net_connect(socket, 0xffffffff, 67)) return 0; @@ -732,16 +748,21 @@ struct socket *net_open(enum socket_type type) return NULL; socket->type = type; + socket->state = S_OPEN; return socket; } void net_close(struct socket *socket) { - socket_close(socket); + if (socket) + socket_close(socket); } int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port) { + if (!socket || socket->state != S_OPEN) + return 0; + socket->ip_addr = ip_addr; socket->dst_port = dst_port; if (!socket->src_port) @@ -752,31 +773,39 @@ int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port) socket->prot.tcp.seq_no = rand(); socket->prot.tcp.ack_no = 0; socket->prot.tcp.state = 0; + socket->state = S_CONNECTING; tcp_send_packet(socket, TCP_FLAG_SYN, NULL, 0); - struct tcp_socket *tcp = &socket->prot.tcp; sti(); u32 time = timer_get(); - while (tcp->state != 3 && tcp->state != 5 && timer_get() - time < 1000) + while (socket->state != S_CONNECTED && timer_get() - time < 1000) ; cli(); - if (tcp->state != 3 && tcp->state != 5) + if (socket->state != S_CONNECTED) { + socket->state = S_FAILED; return 0; + } + return 1; } else if (socket->type == S_UDP) { + socket->state = S_CONNECTED; return 1; } else { + socket->state = S_FAILED; return 0; } - - return 1; } void net_send(struct socket *socket, void *data, u32 len) { + if (!socket || socket->state != S_CONNECTED) + return; + if (socket->type == S_TCP) { tcp_send_packet(socket, TCP_FLAG_PSH | TCP_FLAG_ACK, data, len); socket->prot.tcp.ack_no += len; } else if (socket->type == S_UDP) { udp_send_packet(socket, data, len); + } else { + print("Unknown socket type!\n"); } } @@ -801,7 +830,19 @@ void net_install(void) return; } + // DHCP timeout (no gateway address) u32 time = timer_get(); + while (!gateway_addr && timer_get() - time < 1000) + timer_wait(10); + + if (timer_get() - time >= 1000) { + print("DHCP timeout\n"); + loop(); + return; + } + + // ARP lookup timeout + time = timer_get(); while (!arp_lookup(gateway_mac, gateway_addr) && timer_get() - time < 1000) timer_wait(10); diff --git a/kernel/inc/interrupts.h b/kernel/inc/interrupts.h index 61d31fc..1c475b1 100644 --- a/kernel/inc/interrupts.h +++ b/kernel/inc/interrupts.h @@ -25,9 +25,6 @@ struct idt_ptr { void *base; } __attribute__((packed)); -struct idt_entry idt[256]; -struct idt_ptr idt_ptr; - void idt_set_gate(u8 num, u32 base, u16 sel, u8 flags); void irq_install_handler(int irq, void (*handler)(struct regs *r)); diff --git a/kernel/inc/net.h b/kernel/inc/net.h index fe3270b..f9986e0 100644 --- a/kernel/inc/net.h +++ b/kernel/inc/net.h @@ -161,12 +161,13 @@ struct tcp_socket { // TODO: Use actual socket types (stream etc) enum socket_type { S_TCP, S_UDP }; +enum socket_state { S_CONNECTING, S_CONNECTED, S_OPEN, S_CLOSED, S_FAILED }; struct socket { u32 ip_addr; u32 dst_port; u32 src_port; - u32 status; + enum socket_state state; enum socket_type type; u32 bytes_available; u32 bytes_read; @@ -58,29 +58,29 @@ int memcmp(const void *s1, const void *s2, u32 n) #ifdef kernel -/* #define ALIGNMENT 1ul */ -/* #define ALIGN_TYPE char */ -/* #define ALIGN_INFO sizeof(ALIGN_TYPE) * 16 */ - -/* #define ALIGN(ptr) \ */ -/* if (ALIGNMENT > 1) { \ */ -/* u32 diff; \ */ -/* ptr = (void *)((u32)ptr + ALIGN_INFO); \ */ -/* diff = (u32)ptr & (ALIGNMENT - 1); \ */ -/* if (diff != 0) { \ */ -/* diff = ALIGNMENT - diff; \ */ -/* ptr = (void *)((u32)ptr + diff); \ */ -/* } \ */ -/* *((ALIGN_TYPE *)((u32)ptr - ALIGN_INFO)) = diff + ALIGN_INFO; \ */ -/* } */ - -/* #define UNALIGN(ptr) \ */ -/* if (ALIGNMENT > 1) { \ */ -/* u32 diff = *((ALIGN_TYPE *)((u32)ptr - ALIGN_INFO)); \ */ -/* if (diff < (ALIGNMENT + ALIGN_INFO)) { \ */ -/* ptr = (void *)((u32)ptr - diff); \ */ -/* } \ */ -/* } */ +#define ALIGNMENT 16ul +#define ALIGN_TYPE char +#define ALIGN_INFO sizeof(ALIGN_TYPE) * 16 + +#define ALIGN(ptr) \ + if (ALIGNMENT > 1) { \ + u32 diff; \ + ptr = (void *)((u32)ptr + ALIGN_INFO); \ + diff = (u32)ptr & (ALIGNMENT - 1); \ + if (diff != 0) { \ + diff = ALIGNMENT - diff; \ + ptr = (void *)((u32)ptr + diff); \ + } \ + *((ALIGN_TYPE *)((u32)ptr - ALIGN_INFO)) = diff + ALIGN_INFO; \ + } + +#define UNALIGN(ptr) \ + if (ALIGNMENT > 1) { \ + u32 diff = *((ALIGN_TYPE *)((u32)ptr - ALIGN_INFO)); \ + if (diff < (ALIGNMENT + ALIGN_INFO)) { \ + ptr = (void *)((u32)ptr - diff); \ + } \ + } static u32 *heap; static u32 index; @@ -110,13 +110,13 @@ void *malloc(u32 size) if (size < 1) return NULL; - /* size = size + ALIGNMENT + ALIGN_INFO; */ + size = size + ALIGNMENT + ALIGN_INFO; heap[index] = size; index += size + 1; void *p = (void *)(heap + index - size); - /* ALIGN(p); */ + ALIGN(p); return p; } |