Merge branch 'main' of git.chauvet.pro:starnakin/42_KFS

This commit is contained in:
0x35c 2025-04-18 17:20:43 +02:00
commit 11125325ca
8 changed files with 84 additions and 21 deletions

View File

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

View File

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

View File

@ -38,7 +38,7 @@ void clock_init(struct registers *regs)
static void clock_handler(struct registers *regs)
{
(void)regs;
if (scheduler_counter % 100 == 0)
if (scheduler_counter % 10 == 0)
scheduler();
scheduler_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();
}
static void bozo(int int_code)
{
kprintf("apagnan code\n");
}
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
{
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);
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();
}

View File

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

View File

@ -49,6 +49,9 @@ struct task *create_task(uint8_t uid)
new_task->next = current_task->next;
new_task->prev = current_task;
current_task->next = new_task;
new_task->signals.pending = SIG_IGN;
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->prev = new_task;
new_task->next = new_task;
new_task->signals.pending = SIG_IGN;
current_task = new_task;
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;
}
}