summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-02-19 18:52:30 +0100
committerAnton Kling <anton@kling.gg>2024-02-19 18:52:30 +0100
commit239e33019117f4ab59dd26550d7969e45c290166 (patch)
treede180effd804573d07acf8ac495fa7defc1edd70 /kernel
parent61c443579150a4714c132ecb1ac18779535f91c9 (diff)
Kernel/VFS: Cleanup of file descriptor halts
Diffstat (limited to 'kernel')
-rw-r--r--kernel/Makefile4
-rw-r--r--kernel/drivers/pit.c4
-rw-r--r--kernel/halts.c105
-rw-r--r--kernel/halts.h18
-rw-r--r--kernel/init/kernel.c2
-rw-r--r--kernel/poll.c43
-rw-r--r--kernel/sched/scheduler.c17
-rw-r--r--kernel/sched/scheduler.h11
8 files changed, 66 insertions, 138 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index 3a88d91..c42c95c 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,7 +1,7 @@
CC="i686-sb-gcc"
AS="i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o
-CFLAGS = -O0 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o
+CFLAGS = -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
#LDFLAGS=-flto -Ofast
LDFLAGS=
INCLUDE=-I./includes/ -I../include/ -I./libc/include/
diff --git a/kernel/drivers/pit.c b/kernel/drivers/pit.c
index 108529d..1e9048f 100644
--- a/kernel/drivers/pit.c
+++ b/kernel/drivers/pit.c
@@ -44,11 +44,11 @@ void set_pit_count(u16 hertz) {
void int_clock(reg_t regs) {
outb(0x20, 0x20);
pit_counter++;
- if (pit_counter >= hertz / 1000) {
+ if (pit_counter*1000 >= hertz) {
pit_counter = 0;
clock_num_ms_ticks++;
+ switch_task();
}
- switch_task();
}
void pit_install(void) {
diff --git a/kernel/halts.c b/kernel/halts.c
index 6f60a79..c2b682e 100644
--- a/kernel/halts.c
+++ b/kernel/halts.c
@@ -2,98 +2,37 @@
#include <halts.h>
#include <sched/scheduler.h>
-int create_disconnect_inode_halt(vfs_inode_t *inode) {
- volatile process_t *p = get_current_task();
- int i;
- for (i = 0; i < 100; i++)
- if (!p->disconnect_halt_inode[i])
- break;
-
- if (p->disconnect_halt_inode[i])
- return -1;
-
- p->disconnect_halt_inode[i] = inode;
- return i;
-}
-
-int create_disconnect_fdhalt(vfs_fd_t *fd) {
- assert(fd);
- return create_disconnect_inode_halt(fd->inode);
-}
-
-int create_read_inode_halt(vfs_inode_t *inode) {
- volatile process_t *p = get_current_task();
- int i;
- for (i = 0; i < 100; i++)
- if (!p->read_halt_inode[i])
- break;
-
- if (p->read_halt_inode[i])
- return -1;
-
- p->read_halt_inode[i] = inode;
-
- return i;
-}
-
-int create_read_fdhalt(vfs_fd_t *fd) {
- assert(fd);
- return create_read_inode_halt(fd->inode);
-}
-
-int create_write_inode_halt(vfs_inode_t *inode) {
- volatile process_t *p = get_current_task();
- int i;
- for (i = 0; i < 100; i++)
- if (!p->write_halt_inode[i])
- break;
-
- if (p->write_halt_inode[i])
- return -1;
-
- p->write_halt_inode[i] = inode;
- return i;
-}
-
-int create_write_fdhalt(vfs_fd_t *fd) {
- return create_write_inode_halt(fd->inode);
-}
-
-void unset_read_fdhalt(int i) {
- get_current_task()->read_halt_inode[i] = NULL;
-}
-
-void unset_write_fdhalt(int i) {
- get_current_task()->write_halt_inode[i] = NULL;
-}
-
-void unset_disconnect_fdhalt(int i) {
- get_current_task()->disconnect_halt_inode[i] = NULL;
-}
-
-int isset_fdhalt(vfs_inode_t *read_halts[], vfs_inode_t *write_halts[],
- vfs_inode_t *disconnect_halts[]) {
+int isset_fdhalt(process_t *p) {
int blocked = 0;
- for (int i = 0; i < 100; i++) {
- if (!read_halts[i])
- continue;
- if (read_halts[i]->has_data) {
+ struct list *read_list = &p->read_list;
+ for (int i = 0;; i++) {
+ vfs_inode_t *inode;
+ if (!list_get(read_list, i, (void **)&inode)) {
+ break;
+ }
+ if (inode->has_data) {
return 0;
}
blocked = 1;
}
- for (int i = 0; i < 100; i++) {
- if (!write_halts[i])
- continue;
- if (write_halts[i]->can_write) {
+ struct list *write_list = &p->write_list;
+ for (int i = 0;; i++) {
+ vfs_inode_t *inode;
+ if (!list_get(write_list, i, (void **)&inode)) {
+ break;
+ }
+ if (inode->can_write) {
return 0;
}
blocked = 1;
}
- for (int i = 0; i < 100; i++) {
- if (!disconnect_halts[i])
- continue;
- if (!disconnect_halts[i]->is_open) {
+ struct list *disconnect_list = &p->disconnect_list;
+ for (int i = 0;; i++) {
+ vfs_inode_t *inode;
+ if (!list_get(disconnect_list, i, (void **)&inode)) {
+ break;
+ }
+ if (inode->is_open) {
return 0;
}
blocked = 1;
diff --git a/kernel/halts.h b/kernel/halts.h
index 11e47ff..ed0637f 100644
--- a/kernel/halts.h
+++ b/kernel/halts.h
@@ -1,21 +1,7 @@
#ifndef HALTS_H
#define HALTS_H
#include <fs/vfs.h>
+#include <sched/scheduler.h>
#include <typedefs.h>
-
-typedef struct {
- u8 *ptr;
- u8 active;
-} halt_t;
-
-int create_read_fdhalt(vfs_fd_t *fd);
-int create_read_inode_halt(vfs_inode_t *inode);
-void unset_read_fdhalt(int i);
-int create_write_fdhalt(vfs_fd_t *fd);
-int create_write_inode_halt(vfs_inode_t *inode);
-void unset_write_fdhalt(int i);
-int create_disconnect_fdhalt(vfs_fd_t *fd);
-void unset_disconnect_fdhalt(int i);
-int isset_fdhalt(vfs_inode_t *read_halts[], vfs_inode_t *write_halts[],
- vfs_inode_t *disconnect_halts[]);
+int isset_fdhalt(process_t *p);
#endif
diff --git a/kernel/init/kernel.c b/kernel/init/kernel.c
index 45cb9db..6b160a6 100644
--- a/kernel/init/kernel.c
+++ b/kernel/init/kernel.c
@@ -67,7 +67,7 @@ void kernel_main(u32 kernel_end, unsigned long magic, unsigned long addr,
klog("Syscalls Initalized", LOG_SUCCESS);
pit_install();
- set_pit_count(2000);
+ set_pit_count(500);
klog("PIT driver installed", LOG_SUCCESS);
ata_init();
diff --git a/kernel/poll.c b/kernel/poll.c
index 7e9c8e8..0452bc8 100644
--- a/kernel/poll.c
+++ b/kernel/poll.c
@@ -1,14 +1,19 @@
#include <fs/vfs.h>
#include <halts.h>
+#include <interrupts.h>
+#include <lib/list.h>
#include <poll.h>
#include <sched/scheduler.h>
int poll(struct pollfd *fds, size_t nfds, int timeout) {
(void)timeout;
int rc = 0;
- int read_locks[nfds];
- int write_locks[nfds];
- int disconnect_locks[nfds];
+
+ disable_interrupts();
+
+ struct list *read_list = &get_current_task()->read_list;
+ struct list *write_list = &get_current_task()->write_list;
+ struct list *disconnect_list = &get_current_task()->disconnect_list;
for (size_t i = 0; i < nfds; i++) {
if (fds[i].fd < 0)
continue;
@@ -16,26 +21,25 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
if (NULL == f) {
continue;
}
- if (fds[i].events & POLLIN)
- read_locks[i] = create_read_fdhalt(f);
- if (fds[i].events & POLLOUT)
- write_locks[i] = create_write_fdhalt(f);
- if (fds[i].events & POLLHUP)
- disconnect_locks[i] = create_disconnect_fdhalt(f);
+
+ if (fds[i].events & POLLIN) {
+ list_add(read_list, f->inode);
+ }
+ if (fds[i].events & POLLOUT) {
+ list_add(write_list, f->inode);
+ }
+ if (fds[i].events & POLLHUP) {
+ list_add(disconnect_list, f->inode);
+ }
}
switch_task();
+ disable_interrupts();
+
+ list_reset(read_list);
+ list_reset(write_list);
+ list_reset(disconnect_list);
- for (size_t i = 0; i < nfds; i++) {
- if (fds[i].fd < 0)
- continue;
- if (fds[i].events & POLLIN)
- unset_read_fdhalt(read_locks[i]);
- if (fds[i].events & POLLOUT)
- unset_write_fdhalt(write_locks[i]);
- if (fds[i].events & POLLHUP)
- unset_disconnect_fdhalt(disconnect_locks[i]);
- }
for (size_t i = 0; i < nfds; i++) {
if (0 > fds[i].fd) {
fds[i].revents = 0;
@@ -56,5 +60,6 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
rc++;
}
}
+ enable_interrupts();
return rc;
}
diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c
index 8cb5af7..5a95c4c 100644
--- a/kernel/sched/scheduler.c
+++ b/kernel/sched/scheduler.c
@@ -119,6 +119,10 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) {
r->tcb = kcalloc(1, sizeof(struct TCB));
r->tcb->CR3 = r->cr3->physical_address;
+ list_init(&r->read_list);
+ list_init(&r->write_list);
+ list_init(&r->disconnect_list);
+
// Temporarily switch to the page directory to be able to place the
// "entry_instruction_pointer" onto the stack.
@@ -155,12 +159,6 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) {
} else {
r->file_descriptors[i] = NULL;
}
- if (i < 20)
- r->signal_handlers[i] = NULL;
- r->read_halt_inode[i] = NULL;
- r->write_halt_inode[i] = NULL;
- r->disconnect_halt_inode[i] = NULL;
- r->maps[i] = NULL;
}
return r;
}
@@ -332,8 +330,7 @@ int is_halted(process_t *process) {
if (process->halts[i])
return 1;
- if (isset_fdhalt(process->read_halt_inode, process->write_halt_inode,
- process->disconnect_halt_inode)) {
+ if (isset_fdhalt(process)) {
return 1;
}
return 0;
@@ -349,7 +346,7 @@ process_t *next_task(process_t *c) {
if (1 == loop) {
return s;
}
- loop = 1;
+ // loop = 1;
}
c = c->next;
if (!c)
@@ -387,8 +384,8 @@ void switch_task() {
if (!current_task) {
return;
}
- disable_interrupts();
+ disable_interrupts();
current_task = next_task((process_t *)current_task);
active_directory = current_task->cr3;
diff --git a/kernel/sched/scheduler.h b/kernel/sched/scheduler.h
index 0580ba0..f185e0f 100644
--- a/kernel/sched/scheduler.h
+++ b/kernel/sched/scheduler.h
@@ -1,9 +1,11 @@
+typedef struct Process process_t;
#ifndef SCHEDULER_H
#define SCHEDULER_H
#include <fs/ext2.h>
#include <fs/vfs.h>
#include <halts.h>
#include <ipc.h>
+#include <lib/list.h>
#include <lib/stack.h>
#include <mmu.h>
#include <signal.h>
@@ -37,8 +39,6 @@ typedef struct {
uintptr_t handler_ip;
} signal_t;
-typedef struct Process process_t;
-
typedef struct TCB {
uint32_t ESP;
uint32_t CR3;
@@ -64,9 +64,10 @@ struct Process {
PageDirectory *cr3;
struct IpcMailbox ipc_mailbox;
vfs_fd_t *file_descriptors[100];
- vfs_inode_t *read_halt_inode[100];
- vfs_inode_t *write_halt_inode[100];
- vfs_inode_t *disconnect_halt_inode[100];
+
+ struct list read_list;
+ struct list write_list;
+ struct list disconnect_list;
struct stack restore_context_stack;
struct stack signal_stack;