Added kill syscall

This commit is contained in:
Xnoe 2022-02-14 14:37:48 +00:00
parent 2f2a7a3e45
commit 5c05866d5d
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
6 changed files with 65 additions and 6 deletions

View File

@ -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" : : :);
}

View File

@ -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);

View File

@ -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<Process*> p = Global::kernel->pid_map->get(esi);
if (p.is_ok()) {
Process* proc = p.get();
Global::kernel->destroyProcess(proc);
}
asm("sti");
break;
}
default:

View File

@ -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;

View File

@ -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<AllocTracker>* next = allocations.start;
while (next) {
xnoe::linkedlistelem<AllocTracker>* 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;
}

View File

@ -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 <filename>\n", selectedBuf);
writeStrToBuf(" Loads and executes the program <filename>\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);
}
}