From edf86881446717b633dbb2aaac7b25dbc4630c3f Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Wed, 26 Jun 2024 21:17:06 +0200 Subject: Kernel: Code cleanup --- kernel/Makefile | 1 + kernel/arch/i386/mmu.c | 2 +- kernel/drivers/pit.c | 2 +- kernel/drivers/vbe.c | 3 ++- kernel/elf.c | 1 + kernel/fs/ext2.c | 1 + kernel/fs/shm.c | 8 ++++---- kernel/fs/tmpfs.c | 1 + kernel/fs/vfs.c | 2 +- kernel/kmalloc.c | 2 +- kernel/kubsan.c | 2 +- kernel/libc/exit/assert.c | 2 +- kernel/libc/string/copy.c | 4 ++-- kernel/network/tcp.c | 30 +++++------------------------- kernel/random.c | 1 + kernel/socket.c | 1 + kernel/syscalls/munmap.c | 2 +- 17 files changed, 26 insertions(+), 39 deletions(-) (limited to 'kernel') diff --git a/kernel/Makefile b/kernel/Makefile index 149b1bc..701e43c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -8,6 +8,7 @@ INCLUDE=-I./includes/ -I../include/ -I./libc/include/ all: myos.iso %.o: %.c + clang-format -i $< $(CC) $(INCLUDE) -c -o $@ $< $(CFLAGS) %.o: %.s diff --git a/kernel/arch/i386/mmu.c b/kernel/arch/i386/mmu.c index 6948a73..06d00cc 100644 --- a/kernel/arch/i386/mmu.c +++ b/kernel/arch/i386/mmu.c @@ -659,7 +659,7 @@ int create_table(int table_index) { if (kernel_directory->tables[table_index]) { return 0; } - u32 physical; + u32 physical = 0; kernel_directory->tables[table_index] = (PageTable *)kmalloc_align(sizeof(PageTable), (void **)&physical); if (!kernel_directory->tables[table_index]) { diff --git a/kernel/drivers/pit.c b/kernel/drivers/pit.c index f8d2c42..80f55aa 100644 --- a/kernel/drivers/pit.c +++ b/kernel/drivers/pit.c @@ -50,7 +50,7 @@ void int_clock(reg_t *regs) { if (switch_counter >= hertz) { EOI(0x20); switch_counter = 0; - if(is_switching_tasks) { + if (is_switching_tasks) { return; } switch_task(); diff --git a/kernel/drivers/vbe.c b/kernel/drivers/vbe.c index 5621b63..05edfb8 100644 --- a/kernel/drivers/vbe.c +++ b/kernel/drivers/vbe.c @@ -52,7 +52,8 @@ vfs_vm_object_t *vbe_get_vm_object(u64 length, u64 offset, vfs_fd_t *fd) { (void)length; (void)offset; int n = (uintptr_t)align_page((void *)(u32)framebuffer_size) / 0x1000; - vbe_vm_object.object = kmalloc(sizeof(void *) * n); + + vbe_vm_object.object = kcalloc(sizeof(void *), n); if (!vbe_vm_object.object) { return NULL; } diff --git a/kernel/elf.c b/kernel/elf.c index 3c7f96e..8373818 100644 --- a/kernel/elf.c +++ b/kernel/elf.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/kernel/fs/ext2.c b/kernel/fs/ext2.c index 5416cab..47d4d56 100644 --- a/kernel/fs/ext2.c +++ b/kernel/fs/ext2.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/kernel/fs/shm.c b/kernel/fs/shm.c index ec7286f..8fcb64c 100644 --- a/kernel/fs/shm.c +++ b/kernel/fs/shm.c @@ -87,10 +87,10 @@ int shm_open(const char *name, int oflag, mode_t mode) { } vfs_inode_t *inode = vfs_create_inode( - 0 /*inode_num*/, 0 /*type*/, NULL, NULL, 1 /*is_open*/, - internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, - shm_read, shm_write, NULL /*close*/, NULL /*create_directory*/, - shm_get_vm_object, shm_ftruncate, NULL /*stat*/, NULL /*send_signal*/, NULL /*connect*/); + 0 /*inode_num*/, 0 /*type*/, NULL, NULL, 1 /*is_open*/, internal_object, + 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, shm_read, shm_write, + NULL /*close*/, NULL /*create_directory*/, shm_get_vm_object, + shm_ftruncate, NULL /*stat*/, NULL /*send_signal*/, NULL /*connect*/); vfs_fd_t *fd_ptr; int fd = vfs_create_fd(oflag, mode, 0 /*is_tty*/, inode, &fd_ptr); diff --git a/kernel/fs/tmpfs.c b/kernel/fs/tmpfs.c index cd48b93..8269364 100644 --- a/kernel/fs/tmpfs.c +++ b/kernel/fs/tmpfs.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c index dfdaf44..5bbca8e 100644 --- a/kernel/fs/vfs.c +++ b/kernel/fs/vfs.c @@ -1,9 +1,9 @@ #include #include +#include #include #include #include -#include vfs_inode_t *root_dir; vfs_mounts_t mounts[10]; diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index b7c5dec..5c399e6 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -9,7 +9,7 @@ #include #define NEW_ALLOC_SIZE 0x5000 -//#define KMALLOC_DEBUG +// #define KMALLOC_DEBUG #define IS_FREE (1 << 0) #define IS_FINAL (1 << 1) diff --git a/kernel/kubsan.c b/kernel/kubsan.c index 9bc2631..2923268 100644 --- a/kernel/kubsan.c +++ b/kernel/kubsan.c @@ -1,8 +1,8 @@ +#include #include #include #include #include -#include void ubsan_log(const char *cause, struct source_location source) { kprintf("%s: %s : %d\n", cause, source.file_name, source.line); diff --git a/kernel/libc/exit/assert.c b/kernel/libc/exit/assert.c index 89aa863..fd5f086 100644 --- a/kernel/libc/exit/assert.c +++ b/kernel/libc/exit/assert.c @@ -1,7 +1,7 @@ #include +#include #include #include -#include __attribute__((__noreturn__)) void aFailed(char *f, int l) { kprintf("Assert failed\n"); diff --git a/kernel/libc/string/copy.c b/kernel/libc/string/copy.c index 3ab5618..17f720c 100644 --- a/kernel/libc/string/copy.c +++ b/kernel/libc/string/copy.c @@ -1,8 +1,8 @@ #include #include +#include #include #include -#include char *copy_and_allocate_string(const char *s) { size_t l = strlen(s) + 1; @@ -26,6 +26,6 @@ char *copy_and_allocate_user_string(const char *s) { if (!r) { return NULL; } - memcpy(r, s, len+1); + memcpy(r, s, len + 1); return r; } diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c index d8538cc..bb48f53 100644 --- a/kernel/network/tcp.c +++ b/kernel/network/tcp.c @@ -223,24 +223,6 @@ void handle_tcp(ipv4_t src_ip, ipv4_t dst_ip, const u8 *payload, con->rcv_nxt = seq_num; } - /* - u32 seq_num_end = seq_num + tcp_payload_length - 1; - int case1 = - (con->rcv_nxt <= seq_num) && (seq_num < (con->rcv_nxt + con->rcv_wnd)); - int case2 = (con->rcv_nxt <= seq_num_end) && - (seq_num_end < (con->rcv_nxt + con->rcv_wnd)); - - if (!case1 && !case2) { - kprintf("seq_num: %d\n", seq_num); - kprintf("seq_num_end: %d\n", seq_num_end); - kprintf("con->rcv_nxt: %d\n", con->rcv_nxt); - kprintf("con->rcv_wnd: %d\n", con->rcv_wnd); - // Invalid segment - kprintf("invalid segment\n"); - return; - } - */ - if (ack_num > con->snd_max) { // TODO: Odd ACK number, what should be done? kprintf("odd ACK\n"); @@ -257,16 +239,9 @@ void handle_tcp(ipv4_t src_ip, ipv4_t dst_ip, const u8 *payload, return; } - // kprintf("seq_num: %d rcv_nxt %d\n", seq_num, con->rcv_nxt); - // kprintf("tcp_payload_length: %d\n", tcp_payload_length); - con->snd_wnd = window_size; con->snd_una = ack_num; - // con->sent_ack = - // max(con->sent_ack, seq_num + - // min(ringbuffer_unused(&con->incoming_buffer), - // tcp_payload_length)); con->rcv_nxt += (FIN & flags) ? 1 : 0; con->rcv_nxt += (SYN & flags) ? 1 : 0; @@ -335,6 +310,11 @@ void handle_tcp(ipv4_t src_ip, ipv4_t dst_ip, const u8 *payload, } break; } + case TCP_STATE_CLOSE_WAIT: { + // Waiting for this machine to close the connection. There is + // nothing to respond with. + break; + } default: { klog(LOG_WARN, "TCP state not handled %d", con->state); break; diff --git a/kernel/random.c b/kernel/random.c index 80337bc..7c868ed 100644 --- a/kernel/random.c +++ b/kernel/random.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/kernel/socket.c b/kernel/socket.c index ad774e2..bd7f755 100644 --- a/kernel/socket.c +++ b/kernel/socket.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/kernel/syscalls/munmap.c b/kernel/syscalls/munmap.c index 82f7918..d57ef36 100644 --- a/kernel/syscalls/munmap.c +++ b/kernel/syscalls/munmap.c @@ -2,5 +2,5 @@ #include int syscall_munmap(void *addr, size_t length) { -return munmap(addr, length); + return munmap(addr, length); } -- cgit v1.2.3