Fixed context_switch's handling of the processes linked list, update idt.cpp to add getPID syscall, update memory.cpp to fix but where find_next_available_from would incorrectly return invalid memory locations

This commit is contained in:
Xnoe 2021-12-08 18:49:25 +00:00
parent ca131c71e8
commit 57768784a6
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
3 changed files with 19 additions and 22 deletions

View File

@ -68,8 +68,7 @@ __attribute__((interrupt)) void context_switch(interrupt_frame* frame) {
// This cursed bit of code first determines if the processes list is longer than 1 and if it is
// - Determines if it has 2 or more elements
// - If it has two, swap the first and last, update prev and next of each to be null or the other item
// - If it has more than two, swap the first and last, then swap their next and prevs, and set the
// other value to null
// - If it has more than two, add the start to the end then set start to the second element
if (processes->start->next != 0) {
if (processes->end->prev == processes->start) {
xnoe::linkedlistelem<Process*>* tmp = processes->start;
@ -81,18 +80,13 @@ __attribute__((interrupt)) void context_switch(interrupt_frame* frame) {
processes->end->prev = processes->start;
processes->start->next = processes->end;
} else {
xnoe::linkedlistelem<Process*>* tmp = processes->start;
processes->start = processes->end;
processes->end = tmp;
processes->start->next = processes->end->next;
processes->end->prev = processes->start->prev;
processes->end->next = processes->start;
processes->start = processes->start->next;
processes->start->prev = 0;
xnoe::linkedlistelem<Process*>* tmp = processes->end;
processes->end = processes->end->next;
processes->end->next = 0;
processes->start->next->prev = processes->start;
processes->end->prev->next = processes->end;
processes->end->prev = tmp;
}
}
@ -142,6 +136,7 @@ __attribute__((interrupt)) void syscall(interrupt_frame* frame) {
// 5: localdelete: LocalDelete: Deallocate under current process (in esi: pointer)
// 6: filesize: Get file size (in esi: char* filename; out eax size bytes)
// 7: fork: create process from filename (in esi: char* filename)
// 8: getPID: returns the current process's PID (out eax: uint32_t)
uint32_t* ebp;
asm("mov %%ebp, %0" : "=a" (ebp) :);
@ -179,6 +174,9 @@ __attribute__((interrupt)) void syscall(interrupt_frame* frame) {
Global::kernel->createProcess(esi);
break;
}
case 8:
rval = Global::currentProc->PID;
break;
default:
break;
}

View File

@ -47,9 +47,6 @@ int main() {
Global::currentProc = &kernel;
Process* p1 = kernel.createProcess("WORLD BIN");
Process* p2 = kernel.createProcess("HELLO BIN");
Process* p3 = kernel.createProcess("HELLO BIN");
init_keyboard();

View File

@ -73,15 +73,17 @@ uint32_t PageMap::find_next_available_from(uint32_t address) {
}
uint32_t PageMap::find_next_available_from(uint32_t address, uint32_t count) {
while (1) {
while (!available(address)) address += 4096;
restart:
while (!available(address)) address += 4096;
for (int a=address, i=0; i<count; i++, a+=4096)
if (!available(a))
continue;
return address;
for (int a=address, i=0; i<count; i++, a+=4096) {
if (!available(a)) {
address = a;
goto restart;
}
}
return address;
}
PageTable::PageTable(uint32_t phys, uint32_t virt) {