summaryrefslogtreecommitdiff
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c52
1 files changed, 49 insertions, 3 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index ae2fb38..c502024 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -1,4 +1,5 @@
#include <asm/system.h>
+#include <errno.h>
#include <kernel/con.h>
#include <kernel/kernel.h>
#include <kernel/memory.h>
@@ -6,6 +7,7 @@
#include <signal.h>
#include <string.h>
#include <sys/types.h>
+#include <unistd.h>
/*
* WARNING!
@@ -75,7 +77,7 @@ struct task_struct *create_proc(void *bin, size_t len) {
for(i = 0; i < NRTASKS; i++) {
if(tasks[i].pid == nextpid) {
/* TODO: handle PID exhaustion */
- nextpid++;
+ nextpid = (nextpid + 1) % 0x8000;
if(nextpid == 0)
nextpid++;
i = 0;
@@ -168,11 +170,32 @@ struct task_struct *create_proc(void *bin, size_t len) {
"pushf\n" \
"pop %%eax" \
: "=a" (task->tss.eflags) \
- ::);
+ );
return task;
}
+void kill_proc(struct task_struct *task) {
+ int i, j;
+ uint32_t *x;
+
+ task->pid = 0;
+
+ for(i = 0; i < PGENT; i++) {
+ x = map_page((void*) task->tss.cr3);
+ if(!x[i])
+ continue;
+ x = map_page((void*) (x[i] & 0xFFFFF000));
+ for(j = 0; j < PGENT; j++) {
+ if(x[i] & 1)
+ free_physical_page((void*) (x[i] & 0xFFFFF000));
+ }
+ }
+ free_physical_page((void*) task->tss.cr3);
+
+ reschedule();
+}
+
void sched_init(void) {
int i;
struct task_struct *task;
@@ -236,5 +259,28 @@ void wake_up(struct task_struct **task) {
}
void sighandler_default(uint32_t sig) {
- printk("[kernel] Handling signal 0x%02x for PID 0x%04x\n", sig, ctask->pid);
+ switch(sig) {
+ case SIGKILL:
+ kill_proc(ctask);
+ break;
+ default:
+ printk("[kernel] Handling signal 0x%02x for PID 0x%04x\n", sig, ctask->pid);
+ break;
+ }
+}
+
+int sys_kill(pid_t pid, int sig) {
+ int i;
+
+ if(sig == 0)
+ return 0;
+
+ for(i = 0; i < NRTASKS; i++) {
+ if(tasks[i].pid == pid) {
+ tasks[i].signal |= (1 << (sig - 1));
+ return 0;
+ }
+ }
+
+ return -ESRCH;
}