add signal api

This commit is contained in:
starnakin 2025-04-18 17:07:56 +02:00
parent 6c8c158a1f
commit fc79a47957
8 changed files with 84 additions and 21 deletions

View File

@ -1,9 +1,12 @@
#pragma once #pragma once
#define SIG_DFL -1 #include <stdbool.h>
#define SIG_IGN 0 #include <stdint.h>
enum { #define SIG_DFL 0
#define SIG_IGN -1
typedef enum {
SIGABRT, // Abort signal from abort(3) SIGABRT, // Abort signal from abort(3)
SIGALRM, // Timer signal from alarm(2) SIGALRM, // Timer signal from alarm(2)
SIGBUS, // Bus error (bad memory access) SIGBUS, // Bus error (bad memory access)
@ -44,11 +47,16 @@ enum {
SIGXFSZ, // File size limit exceeded (4.2BSD); see setrlimit(2) SIGXFSZ, // File size limit exceeded (4.2BSD); see setrlimit(2)
SIGWINCH, // Window resize signal (4.3BSD, Sun) SIGWINCH, // Window resize signal (4.3BSD, Sun)
SIG_INT, // Interrupt from keyboard SIG_INT, // Interrupt from keyboard
}; LAST
} SIGNAL_CODE;
typedef void (*sighandler_t)(int); typedef void (*signal_handler_t)(int);
struct signal { void signal(SIGNAL_CODE sig_num, void *handler);
int signum; void kill(int pid, SIGNAL_CODE sig_num);
sighandler_t handler; void exec_signal_pending(void);
struct signal_data {
void *handlers[LAST];
uint32_t pending;
}; };

View File

@ -2,6 +2,7 @@
#include "list.h" #include "list.h"
#include "memory.h" #include "memory.h"
#include "signal.h"
#include <stdint.h> #include <stdint.h>
@ -23,7 +24,7 @@ struct task {
uint8_t uid; uint8_t uid;
struct task *daddy; struct task *daddy;
struct task *child; struct task *child;
struct list **signals; struct signal_data signals;
struct task *next; struct task *next;
struct task *prev; struct task *prev;
}; };

View File

@ -38,7 +38,7 @@ void clock_init(struct registers *regs)
static void clock_handler(struct registers *regs) static void clock_handler(struct registers *regs)
{ {
(void)regs; (void)regs;
if (scheduler_counter % 100 == 0) if (scheduler_counter % 10 == 0)
scheduler(); scheduler();
scheduler_counter++; scheduler_counter++;
sleep_counter--; sleep_counter--;

View File

@ -1,10 +0,0 @@
#include "signal.h"
#include <stddef.h>
void (*signal(int sig, void (*func)(int)))(int)
{
// TODO after multi tasking and processes
(void)sig;
(void)func;
return NULL;
}

View File

@ -52,6 +52,11 @@ static void awa(void)
wait(); wait();
} }
static void bozo(int int_code)
{
kprintf("apagnan code\n");
}
void kernel_main(multiboot_info_t *mbd, uint32_t magic) void kernel_main(multiboot_info_t *mbd, uint32_t magic)
{ {
terminal_initialize(); terminal_initialize();
@ -79,5 +84,10 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
// exec_fn(owo); // exec_fn(owo);
// exec_fn(owo); // exec_fn(owo);
// exec_fn(owo); // exec_fn(owo);
kill(0, 4);
for (size_t i = 0; i < 10000; i++)
free_pages(alloc_pages(1, NULL), 1);
signal(4, bozo);
kill(0, 4);
shell_init(); shell_init();
} }

View File

@ -30,5 +30,6 @@ void scheduler(void)
it = it->next; it = it->next;
} }
switch_to_task(it); switch_to_task(it);
exec_signal_pending();
toris(); toris();
} }

View File

@ -49,6 +49,9 @@ struct task *create_task(uint8_t uid)
new_task->next = current_task->next; new_task->next = current_task->next;
new_task->prev = current_task; new_task->prev = current_task;
current_task->next = new_task; current_task->next = new_task;
new_task->signals.pending = SIG_IGN;
return new_task; return new_task;
} }
@ -64,6 +67,7 @@ int8_t create_kernel_task(void)
new_task->cr3 = (uint32_t *)((uint32_t)kernel_pd - HEAP_END); new_task->cr3 = (uint32_t *)((uint32_t)kernel_pd - HEAP_END);
new_task->prev = new_task; new_task->prev = new_task;
new_task->next = new_task; new_task->next = new_task;
new_task->signals.pending = SIG_IGN;
current_task = new_task; current_task = new_task;
return 0; return 0;
} }

49
src/signal/signal.c Normal file
View File

@ -0,0 +1,49 @@
#include "signal.h"
#include "kprintf.h"
#include "task.h"
#include <stdint.h>
void signal(SIGNAL_CODE sig_num, void *handler)
{
current_task->signals.handlers[sig_num] = handler;
}
void kill(int pid, SIGNAL_CODE sig_num)
{
struct task *task = current_task;
while (task->pid != pid)
task = task->next;
task->signals.pending = sig_num;
}
static void display_signal(SIGNAL_CODE sig_num)
{
kprintf("signal: %d\n", sig_num);
}
static void exec_signal(SIGNAL_CODE sig_num)
{
void *handler = current_task->signals.handlers[sig_num];
if (handler == SIG_IGN)
return;
if (handler == SIG_DFL) {
display_signal(sig_num);
return;
}
signal_handler_t func = handler;
func(sig_num);
}
void exec_signal_pending(void)
{
uint32_t signal_code = current_task->signals.pending;
if (signal_code != SIG_IGN) {
exec_signal(signal_code);
current_task->signals.pending = SIG_IGN;
}
}