Page 1 of 1

Triple fault after enabling paging

Posted: Sun Apr 29, 2018 12:12 am
by itsmevjnk
Hi,
After enabling paging my kernel will cause a page fault, then a double fault and a triple fault. This is my paging library:

Code: Select all

#include <kernel/paging.h>

uint32_t pageDirectory[1024] __attribute__((aligned(4096)));
uint32_t* pageTable_ID[1024] __attribute__ ((aligned(4096)));
uint32_t* pageTable_Kernel[1024] __attribute__ ((aligned(4096)));

extern void pagingLoadDir(uint32_t* directory);
extern void pagingEnable();

void pagingCalculateAddress(uint32_t vaddr, uint16_t* dirEntry, uint16_t* tableEntry) {
	*dirEntry = vaddr / 4194304;
	*tableEntry = vaddr % 4194304;
}

void pagingFillTable(void* table, uint32_t* address, uint16_t attributes) {
	uint32_t* tablePtr = (uint32_t*) table;
	for(uint16_t i = 0; i < 1024; i++) {
		tablePtr[i] = address;
		tablePtr[i] |= attributes;
		address += 4096;
	}
}

void pagingAddTable(uint16_t entry, void* table, uint16_t attributes) {
	pageDirectory[entry] = table;
	pageDirectory[entry] |= attributes;
}

void pagingInit(void) {
	for(uint16_t i = 0; i < 1024; i++) pageDirectory[i] = 0;
	
	pagingFillTable(pageTable_ID, 0x00000000, 3);
	pagingAddTable(0, pageTable_ID, 3);
	pagingFillTable(pageTable_Kernel, 0x00100000, 3);
	pagingAddTable(768, pageTable_Kernel, 3);
	
	pagingLoadDir((uint32_t) pageDirectory);
	pagingEnable();
}
The full source code is at https://github.com/weedboi6969/puckos. Can someone help me with that?
These are the guides that I used:
http://www.osdever.net/tutorials/view/i ... sic-paging
https://wiki.osdev.org/Setting_Up_Paging

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 2:55 am
by alexfru

Code: Select all

void pagingFillTable(..., uint32_t* address, ...) {
   ...
   for(...) {
      ...
      address += 4096;
   }
}
Can you spot any problem here?

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 5:36 am
by itsmevjnk
alexfru wrote:

Code: Select all

void pagingFillTable(..., uint32_t* address, ...) {
   ...
   for(...) {
      ...
      address += 4096;
   }
}
Can you spot any problem here?
No. What's wrong with it?

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 5:54 am
by iansjack
If you were to single-step through that code in a debugger you would spot the error immediately.

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 8:27 am
by itsmevjnk
iansjack wrote:If you were to single-step through that code in a debugger you would spot the error immediately.
I can see in Bochs that it's causing a page fault and that's it.

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 8:36 am
by iansjack
Brush up on pointer arithmetic in C.

Re: Triple fault after enabling paging

Posted: Sun Apr 29, 2018 9:54 am
by itsmevjnk
After changing uint32_t* to uint32_t, it works.