diff --git a/src/common/common.c b/src/common/common.c index 697788d..37a95c2 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -48,6 +48,10 @@ void fclose(uint32_t fh) { asm volatile("mov $16, %%eax; mov %0, %%esi; int $0x7f" : : "m" (fh) : "esi"); } +void kill(uint32_t pid) { + asm volatile("mov $17, %%eax; mov %0, %%esi; int $0x7f" : : "m" (pid) : "esi"); +} + void bindToKeyboard() { asm volatile ("mov $12, %%eax; int $0x7f" : : :); } diff --git a/src/common/common.h b/src/common/common.h index b9f8dac..3d12a6b 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -21,6 +21,7 @@ void bindToKeyboard(); int fopen(char* filename); void fclose(uint32_t fh); +void kill(uint32_t pid); int int_to_decimal(unsigned int number, char* string_buffer); int int_to_hex(unsigned int number, char* string_buffer); diff --git a/src/kernel/idt.cpp b/src/kernel/idt.cpp index 1fcc6d0..2a115a2 100644 --- a/src/kernel/idt.cpp +++ b/src/kernel/idt.cpp @@ -167,6 +167,8 @@ void syscall(frame_struct* frame) { // 15: fopen :: char* path esi -> int filehandler // Returns a filehandler to the file. // 16: fclose :: int filehandler esi -> void // Closes a file handler. + // 17: kill :: int PID esi -> void // Destroys a process. + // File handlers: // 0: Stdout // 1: Stdin @@ -288,6 +290,7 @@ void syscall(frame_struct* frame) { case 15: { ReadWriter* file = new FATFileReadWriter(0, esi); rval = Global::kernel->mapFH(file); + break; } case 16: { @@ -296,6 +299,18 @@ void syscall(frame_struct* frame) { delete f.get(); Global::kernel->unmapFH(esi); } + break; + } + + case 17: { + asm("cli"); + xnoe::Maybe p = Global::kernel->pid_map->get(esi); + if (p.is_ok()) { + Process* proc = p.get(); + Global::kernel->destroyProcess(proc); + } + asm("sti"); + break; } default: diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 3a6736a..a9660d4 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -39,6 +39,8 @@ Process* Kernel::createProcess(uint32_t fh, ReadWriter* stdout) { } void Kernel::destroyProcess(Process* p) { + if (Global::currentProc == p) + Global::currentProcValid = false; this->processes.remove(p); this->pid_map->remove(p->PID); delete p; diff --git a/src/kernel/process.cpp b/src/kernel/process.cpp index 4a24aa2..30cc680 100644 --- a/src/kernel/process.cpp +++ b/src/kernel/process.cpp @@ -95,16 +95,20 @@ Process::Process(uint32_t PID, PageDirectory* inherit, uint32_t inheritBase, uin } Process::~Process() { + uint32_t pCR3; + asm ("mov %%cr3, %0" : "=a" (pCR3) :); + this->PD->select(); xnoe::linkedlistelem* next = allocations.start; while (next) { xnoe::linkedlistelem* active = next; next = next->next; - printf("Deleted %x\n", active->elem.page_base); + //printf("Deleted %x\n", active->elem.page_base); this->deallocate(active->elem.page_base+1); } this->deallocate(stack); + asm ("mov %0, %%cr3" : : "r" (pCR3)); delete kernelStackPtr; } diff --git a/src/world/world.c b/src/world/world.c index ad8ff7c..4f7a618 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -49,6 +49,14 @@ void writeToBuf(char c, procbuffer* buf) { } } +void clearBuf(procbuffer* buf) { + for (int i=0; i<21*38;i++) { + buf->buffer[i] = ' '; + } + buf->x = 0; + buf->y = 0; +} + void writeStrToBuf(char* c, procbuffer* b) { char* s = c; while(*s) @@ -203,10 +211,12 @@ int main() { while (1) { char c; - if (read(1, b1.stdout, &c)) - writeToBuf(c, &b1); - if (read(1, b2.stdout, &c)) - writeToBuf(c, &b2); + if (b1.process) + if (read(1, b1.stdout, &c)) + writeToBuf(c, &b1); + if (b2.process) + if (read(1, b2.stdout, &c)) + writeToBuf(c, &b2); if (read(1, 1, &c)) { if (c == ':') { char buf[32] = {0}; @@ -231,9 +241,32 @@ int main() { writeStrToBuf(":load \n", selectedBuf); writeStrToBuf(" Loads and executes the program \n", selectedBuf); writeStrToBuf("--------\n", selectedBuf); + } else if (strcmpcnt(4, buf, "kill")) { + if (selectedBuf->process) { + kill(selectedBuf->process); + clearBuf(selectedBuf); + selectedBuf->process = 0; + selectedBuf->stdin = 0; + selectedBuf->stdout = 0; + if (selectedBuf == &b1) { + selectedBuf = &b2; + } else { + selectedBuf = &b1; + } + } + } else if (strcmpcnt(4, buf, "load")) { + if (!selectedBuf->process) { + char* potFn = buf+5; + uint32_t fh = fopen(potFn); + selectedBuf->process = fork(fh); + selectedBuf->stdout = bindStdout(selectedBuf->process); + selectedBuf->stdin = bindStdin(selectedBuf->process); + fclose(fh); + } } } else { - write(1, selectedBuf->stdin, &c); + if (selectedBuf->process) + write(1, selectedBuf->stdin, &c); } }