Page 1 of 1

Page fault when using kmalloc

Posted: Thu Aug 18, 2016 5:43 am
by FixR
Hi! I'm following updated JamesM's tutorial series(here's a link). I implemented vmm, pmm and heap. But when i use kmalloc(), i get a page fault.

Here's the kmalloc function:

Code: Select all

void *kmalloc (uint32_t l)
{
	l += sizeof (header_t);

	header_t *cur_header = heap_first, *prev_header = 0;
	while (cur_header)
	{
		if (cur_header->allocated == 0 && cur_header->length >= l)
		{
			split_chunk (cur_header, l);
			cur_header->allocated = 1;
			return (void*) ((uint32_t)cur_header + sizeof (header_t));
		}
		prev_header = cur_header;
		cur_header = cur_header->next;
	}

	uint32_t chunk_start;
	if (prev_header) chunk_start = (uint32_t)prev_header + prev_header->length;
	else
	{
		chunk_start = HEAP_START;
		heap_first = (header_t *)chunk_start;
	}

	alloc_chunk (chunk_start, l);
	cur_header = (header_t *)chunk_start;
	cur_header->prev = prev_header;
	cur_header->next = 0;
	cur_header->allocated = 1;
	cur_header->length = l;

	prev_header->next = cur_header;

	return (void*) (chunk_start + sizeof (header_t));
}

Re: Page fault when using kmalloc

Posted: Thu Aug 18, 2016 7:54 am
by FixR
The problem is in alloc_chunk function, because the text doesn't appear.

Code: Select all

alloc_chunk (chunk_start, l);
	printf("It works!");
	cur_header = (header_t *)chunk_start;
	cur_header->prev = prev_header;
	cur_header->next = 0;
	cur_header->allocated = 1;
	cur_header->length = l;
Here's the function:

Code: Select all

void alloc_chunk (uint32_t start, uint32_t len)
{
	while (start + len > heap_max)
	{
		uint32_t page = pmm_alloc_page ();
		map (heap_max, page, PAGE_PRESENT | PAGE_WRITE);
		heap_max += 0x1000;
	}
}

Re: Page fault when using kmalloc

Posted: Thu Aug 18, 2016 7:58 am
by max
You will not get any help by simply posting your code and saying it doesn't work. We're not the support crew of jamesm's tutorial stuff. Probably an issue with your pmm as chunk_alloc calls it, but it's your job to debug that stuff.