diff options
author | Anton Kling <anton@kling.gg> | 2024-10-14 21:42:38 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-10-14 21:42:38 +0200 |
commit | 87af93c7521be331a5794c6a53f31d13b3a24baa (patch) | |
tree | 7807e28e64b17bebd3a5ad4f980fbada3278e6df | |
parent | a5b508d1fb751015ecb9b6701749dcdcd81e3071 (diff) |
kernel: stuff
-rw-r--r-- | kernel/cpu/syscall.c | 8 | ||||
-rw-r--r-- | kernel/fs/shm.c | 24 | ||||
-rw-r--r-- | kernel/fs/vfs.h | 1 | ||||
-rw-r--r-- | kernel/network/udp.c | 3 | ||||
-rw-r--r-- | kernel/sched/scheduler.c | 23 | ||||
-rw-r--r-- | userland/libc/include/stdio.h | 1 | ||||
-rw-r--r-- | userland/libc/include/syscall.h | 1 | ||||
-rw-r--r-- | userland/libc/libc.c | 4 |
8 files changed, 49 insertions, 16 deletions
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c index d31835d..aad0284 100644 --- a/kernel/cpu/syscall.c +++ b/kernel/cpu/syscall.c @@ -362,6 +362,13 @@ int syscall_shm_open(SYS_SHM_OPEN_PARAMS *args) { return shm_open(args->name, args->oflag, args->mode); } +int syscall_shm_unlink(const char *name) { + if (!mmu_is_valid_user_c_string(name, NULL)) { + return -EPERM; // TODO: Is this correct? + } + return shm_unlink(name); +} + int syscall_sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact) { set_signal_handler(sig, act->sa_handler); @@ -692,6 +699,7 @@ int (*syscall_functions[])() = { (void(*))syscall_queue_mod_entries, (void(*))syscall_queue_wait, (void(*))syscall_sendfile, + (void(*))syscall_shm_unlink, }; void int_syscall(reg_t *r); diff --git a/kernel/fs/shm.c b/kernel/fs/shm.c index 57ec3c5..5e44b97 100644 --- a/kernel/fs/shm.c +++ b/kernel/fs/shm.c @@ -78,6 +78,12 @@ int shm_ftruncate(vfs_fd_t *fd, size_t length) { return 0; } +void shm_close(vfs_fd_t *fd) { + vfs_vm_object_t *internal_object = fd->inode->internal_object; + assert(internal_object->num_of_references > 0); + internal_object->num_of_references--; +} + int shm_open(const char *name, int oflag, mode_t mode) { // Try to find or create a new shared memory object. vfs_vm_object_t *internal_object = @@ -90,10 +96,11 @@ int shm_open(const char *name, int oflag, mode_t mode) { hashmap_add_entry(shared_memory_objects, name, internal_object, NULL, 0); } + internal_object->num_of_references++; vfs_inode_t *inode = vfs_create_inode( 0 /*inode_num*/, 0 /*type*/, NULL, NULL, 1 /*is_open*/, 0, internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, - shm_read, shm_write, NULL /*close*/, NULL /*create_directory*/, + shm_read, shm_write, shm_close, NULL /*create_directory*/, shm_get_vm_object, shm_ftruncate, NULL /*stat*/, NULL /*send_signal*/, NULL /*connect*/); @@ -107,6 +114,19 @@ int shm_open(const char *name, int oflag, mode_t mode) { } int shm_unlink(const char *name) { - (void)name; + vfs_vm_object_t *p = hashmap_get_entry(shared_memory_objects, name); + if (!p) { + return -ENOENT; + } + if (0 != p->num_of_references) { + p->num_of_references--; + } + if (0 == p->num_of_references && p->real_pointer) { + u8 *mem_region = p->real_pointer; + mmu_free_address_range(mem_region, align_page(p->size), NULL); + p->real_pointer = NULL; + p->size = 0; + } + assert(hashmap_delete_entry(shared_memory_objects, name)); return 0; } diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h index 640cfbb..396802b 100644 --- a/kernel/fs/vfs.h +++ b/kernel/fs/vfs.h @@ -25,6 +25,7 @@ struct vfs_vm_object { void *virtual_object; void **object; u64 size; + u32 num_of_references; }; struct vfs_mounts { diff --git a/kernel/network/udp.c b/kernel/network/udp.c index c0a5237..6b40c1a 100644 --- a/kernel/network/udp.c +++ b/kernel/network/udp.c @@ -16,12 +16,11 @@ void send_udp_packet(struct sockaddr_in *src, const struct sockaddr_in *dst, header[2] = htons(payload_length + 8); u16 packet_length = sizeof(header) + payload_length; - u8 *packet = kmalloc(packet_length); + u8 packet[0x1000]; memcpy(packet, header, sizeof(header)); memcpy(packet + sizeof(header), payload, payload_length); send_ipv4_packet((ipv4_t){.d = dst->sin_addr.s_addr}, 0x11, packet, packet_length); - kfree(packet); } void handle_udp(ipv4_t src_ip, ipv4_t dst_ip, const u8 *payload, diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c index 1285d26..9fec2b7 100644 --- a/kernel/sched/scheduler.c +++ b/kernel/sched/scheduler.c @@ -28,14 +28,6 @@ u32 next_pid = 0; extern u32 read_eip(void); -process_t *get_current_task(void) { - return current_task; -} - -process_t *get_ready_queue(void) { - return ready_queue; -} - void process_push_restore_context(process_t *p, reg_t r) { if (!p) { p = current_task; @@ -535,20 +527,27 @@ process_t *next_task(process_t *s) { void signal_process(process_t *p, int sig) { assert(sig < 32); - kprintf("sending signal to: %x\n", p); + int talking_to_itself = (p == current_task); if (!p->signal_handlers[sig]) { if (SIGTERM == sig) { kprintf("HAS NO SIGTERM\n"); exit_process(p, 1 /*TODO: what should the status be?*/); - ASSERT_NOT_REACHED; + if (talking_to_itself) { + ASSERT_NOT_REACHED; + } } else if (SIGSEGV == sig) { kprintf("HAS NO SIGSEGV\n"); exit_process(p, 1 /*TODO: what should the status be?*/); - ASSERT_NOT_REACHED; + if (talking_to_itself) { + ASSERT_NOT_REACHED; + } } else { // TODO: Should also exit proess(I think) - ASSERT_NOT_REACHED; + if (talking_to_itself) { + ASSERT_NOT_REACHED; + } } + return; } signal_t signal = {.handler_ip = (uintptr_t)p->signal_handlers[sig]}; process_push_signal(p, signal); diff --git a/userland/libc/include/stdio.h b/userland/libc/include/stdio.h index aebf086..ac49a67 100644 --- a/userland/libc/include/stdio.h +++ b/userland/libc/include/stdio.h @@ -76,6 +76,7 @@ int wait(int *stat_loc); void exit(int status); void *memcpy(void *dest, const void *src, uint32_t n); int shm_open(const char *name, int oflag, mode_t mode); +int shm_unlink(const char *name); int dprintf(int fd, const char *format, ...); int vdprintf(int fd, const char *format, va_list ap); int vprintf(const char *format, va_list ap); diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h index dda4b82..a0303f4 100644 --- a/userland/libc/include/syscall.h +++ b/userland/libc/include/syscall.h @@ -52,6 +52,7 @@ #define SYS_QUEUE_MOD_ENTRIES 44 #define SYS_QUEUE_WAIT 45 #define SYS_SENDFILE 46 +#define SYS_SHM_UNLINK 47 int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi); diff --git a/userland/libc/libc.c b/userland/libc/libc.c index decd002..f1b6bdf 100644 --- a/userland/libc/libc.c +++ b/userland/libc/libc.c @@ -272,3 +272,7 @@ int shm_open(const char *name, int oflag, mode_t mode) { }; RC_ERRNO(syscall(SYS_SHM_OPEN, (u32)&args, 0, 0, 0, 0)); } + +int shm_unlink(const char *name) { + RC_ERRNO(syscall(SYS_SHM_UNLINK, (u32)name, 0, 0, 0, 0)); +} |