Skip to content

Commit

Permalink
Merge pull request #33 from lux-operating-system/ipc-signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes authored Nov 27, 2024
2 parents 3c6fb29 + aaa6fd6 commit c4b09eb
Show file tree
Hide file tree
Showing 18 changed files with 745 additions and 34 deletions.
16 changes: 15 additions & 1 deletion src/include/kernel/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@
#define WNOHANG 0x02
#define WUNTRACED 0x04

typedef struct SignalQueue {
struct SignalQueue *next;
int signum;
struct Thread *sender;
} SignalQueue;

typedef struct Thread {
int status, cpu, priority;
pid_t pid, tid; // pid == tid for the main thread
uint64_t time; // timeslice OR sleep time if sleeping thread
lock_t lock;

bool normalExit; // true when the thread ends by exit() and is not forcefully killed
bool clean; // true when the exit status has been read by waitpid()
bool clean; // true when the exit status has been read by waitpid()
bool handlingSignal; // true inside a signal handler

void *signals;
SignalQueue *signalQueue;
uintptr_t signalTrampoline;
uintptr_t siginfo;
uintptr_t signalUserContext;

SyscallRequest syscall; // for when the thread is blocked
int exitStatus; // for zombie threads
Expand All @@ -56,6 +69,7 @@ typedef struct Thread {

struct Thread *next;
void *context; // platform-specific (page tables, registers, etc)
void *signalContext;

uintptr_t highest;
} Thread;
Expand Down
110 changes: 110 additions & 0 deletions src/include/kernel/signal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* lux - a lightweight unix-like operating system
* Omar Elghoul, 2024
*
* Core Microkernel
*/

#pragma once

#include <sys/types.h>
#include <kernel/sched.h>

/* Signal Handler Macros */
#define SIG_DFL (void (*)(int))0
#define SIG_ERR (void (*)(int))1
#define SIG_HOLD (void (*)(int))2
#define SIG_IGN (void (*)(int))3

#define SIG_T 1 /* terminate */
#define SIG_A 2 /* abort */
#define SIG_C 3 /* continue */
#define SIG_S 4 /* stop */
#define SIG_I 5 /* ignore */

/* ISO C Signals */
#define SIGABRT 1
#define SIGFPE 2
#define SIGILL 3
#define SIGINT 4
#define SIGSEGV 5
#define SIGTERM 6

/* POSIX Extension Signals */
#define SIGALRM 7
#define SIGBUS 8
#define SIGCHLD 9
#define SIGCONT 10
#define SIGHUP 11
#define SIGKILL 12
#define SIGPIPE 13
#define SIGQUIT 14
#define SIGSTOP 15
#define SIGTSTP 16
#define SIGTTIN 17
#define SIGTTOU 18
#define SIGUSR1 19
#define SIGUSR2 20
#define SIGPOLL 21
#define SIGSYS 22
#define SIGTRAP 23
#define SIGURG 24
#define SIGVTALRM 25
#define SIGXCPU 26
#define SIGXFSZ 27

#define MAX_SIGNAL 27

/* Signal Flags */
#define SA_NOCLDSTOP 0x0001
#define SA_ONSTACK 0x0002
#define SA_RESETHAND 0x0004
#define SA_RESTART 0x0008
#define SA_SIGINFO 0x0010
#define SA_NOCLDWAIT 0x0020
#define SA_NODEFER 0x0040

#define SIG_BLOCK 0x0001
#define SIG_UNBLOCK 0x0002

#define SS_ONSTACK 0x0001
#define SS_DISABLE 0x0002

#define MINSIGSTKSZ 4096
#define SIGSTKSZ 16384

typedef volatile uint32_t sig_atomic_t;
typedef uint64_t sigset_t;

typedef struct {
int si_signo, si_code, si_errno, si_status;
pid_t si_pid;
uid_t si_uid;
void *si_addr;
long si_band;
} siginfo_t;

struct sigaction {
union {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
};

sigset_t sa_mask;
int sa_flags;
};

int sigemptyset(sigset_t *);
int sigfillset(sigset_t *);
int sigaddset(sigset_t *, int);
int sigdelset(sigset_t *, int);
int sigismember(sigset_t *, int);

void *signalDefaults();
int signalDefaultHandler(int);
void *signalClone(const void *);
void signalHandle(Thread *);

int kill(Thread *, pid_t, int);
int sigaction(Thread *, int, const struct sigaction *, struct sigaction *);
void sigreturn(Thread *);
3 changes: 2 additions & 1 deletion src/include/kernel/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
#include <stdbool.h>
#include <kernel/sched.h>

#define MAX_SYSCALL 56
#define MAX_SYSCALL 58

/* IPC syscall indexes, this range will be used for immediate handling without
* waiting for the kernel thread to dispatch the syscall */
#define SYSCALL_ACCEPT 44 // accept()
#define SYSCALL_IPC_START 42 // bind()
#define SYSCALL_IPC_END 46 // send()

Expand Down
4 changes: 3 additions & 1 deletion src/include/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ int platformGetMaxIRQ(); // maximum interrupt implemented by hardware
int platformConfigureIRQ(Thread *, int, IRQHandler *); // configure an IRQ pin
IRQCommand *platformGetIRQCommand(); // per-CPU IRQ command structure
void platformIdle(); // to be called when the CPU is idle
void platformCleanThread(void *, uintptr_t); // garbage collector after thread is killed or replaced by exec()
void platformCleanThread(void *, uintptr_t); // garbage collector after thread is killed or replaced by exec()
int platformSendSignal(Thread *, Thread *, int, uintptr_t);
void platformSigreturn(Thread *);
5 changes: 4 additions & 1 deletion src/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ char *itoa(int, char *, int);
int atoi(const char *);
char *ltoa(long, char *, int);
long atol(const char *);
void *mallocUC(size_t);
void *malloc(size_t);
void *calloc(size_t, size_t);
void *realloc(void *, size_t);
void *umalloc(size_t);
void *uxmalloc(size_t);
void *ucalloc(size_t, size_t);
void *urealloc(void *, size_t);
void free(void *);
int rand();
void srand(unsigned int);
Loading

0 comments on commit c4b09eb

Please sign in to comment.