[SOLVED] physical memory manager fails

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

[SOLVED] physical memory manager fails

Post by MadZarx »

Hello agian 8)
I'm working in my bitmap physical memory manager. I could detect the available memory region to write in. But when I want to set all the bits to 1 in the initialization, the kernel freezes and nothing happens. :( I think I'm writing to kernel regions but I'm not sure cuz I don't know how to get the kernel offset and length.
Any help would be appreciated :mrgreen: :)

Edit:
I use qemu and it says:
!new_block->iTrying to execute code outside RAM or ROM at 0x303078cc

This a part of the code I use for initializing the pmm:

Code: Select all

static uint32_t
		_mem_size = 0,
		_mem_used_blocks = 0,
		_mem_max_blocks = 0,
		*_mem_map = 0;

static uint32_t
		_mem_usable_off = 0,
		_mem_usable_len = 0;

static void _pmm_find_available_memory(memory_map *mmap)
{
	uint32_t a = 0, b = 0;
	for (uint16_t i = 0; ; i++)
	{
		if (mmap[i].base_addr_low == 0 && i > 0)
			break;
		
		if (mmap[i].type == 1)
			if (mmap[i].length_low > a)
			{
				a = mmap[i].length_low;
				b = i;
			}
	}
	_mem_usable_len = a;
	_mem_usable_off = mmap[b].base_addr_low;
}

void pmm_initialize(memory_map *mmap)
{
	_pmm_find_available_memory(mmap);
	
	_mem_size = _mem_usable_len;
	_mem_map = (uint32_t*)_mem_usable_off;
	_mem_max_blocks = _mem_size / PMM_BLOCK_SIZE;
	_mem_used_blocks = _mem_max_blocks;
	
	printf("\n\t* Memory size %i Bytes", _mem_size);
	for (uint32_t i = 0; i < 1000; i++) // I used 1000 only for testing
		_mem_map[i] = 0xffffffff;
}
Last edited by MadZarx on Tue Aug 13, 2013 1:34 am, edited 4 times in total.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Memory allocation fail

Post by sortie »

I bet you are triggering a triple fault because you don't have interrupt handlers yet and your code triggered a page fault. If you modified paging entries, be sure to know whether you need to flush the TLB (by reloading cr3).
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: Memory allocation fail

Post by MadZarx »

sortie wrote:I bet you are triggering a triple fault because you don't have interrupt handlers yet and your code triggered a page fault. If you modified paging entries, be sure to know whether you need to flush the TLB (by reloading cr3).
No I have set up the exceptions and IRQs. I don't think there should be any problems with the interrupts. My kernel freezes not triple fault.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: physical memory manager fails

Post by sortie »

Alright good - note that some systems and emulators actually just freeze (in particular VirtualBox, I think also Qemu) when they triple fault. When you say "freeze" what exactly do you mean? The CPU stops executing? The kernel has dead-locked?
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: physical memory manager fails

Post by MadZarx »

When it wants to execute the _mem_map = 0xffffffff; line, it freezes and I should close qemu and then I see a couple of cpu registers information.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: physical memory manager fails

Post by MadZarx »

Can anyone help me please?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: physical memory manager fails

Post by bluemoon »

"It freeze" is not good description on the issue, and the lack of information on paging related stuff (how you enable paging, how you handle #PF, and what exception you traced, etc) means that if anyone want to help you to identify the problem it would be total guess work.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: physical memory manager fails

Post by MadZarx »

I mean the kernel stops working when I want to set all the bits in the bitmap table to 0xf and no exception is caught and I can't do anything else except closing the VM. I haven't enabled paging. I have just started to develop a physical memory manager and I could detect memory regions using multiboot and I placed the bitmap table at the first of the available memory region. I have explained everything. I can capture a video of whats happening if you need more info :idea:
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: physical memory manager fails

Post by bluemoon »

There are a few logical issue on your code.

1. you can't ignore high dword, it may be validate for FFFF0000:00000000 but your code break.
2. similar to (1), your code treat FFFFFFFF:FF000000 as FF000000, a different address.
3. same holds for length
4. As you suspected, you may be writing to the kernel regions, you can verify this by checking the value of _mem_map against the object layout from objdump.
MadZarx wrote:I don't know how to get the kernel offset and length
There are more than one way to do this, the loader may pass such info to the kernel, or otherwise you may ask the linker to inject labels.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: physical memory manager fails

Post by MadZarx »

bluemoon wrote: the loader may pass such info to the kernel, or otherwise you may ask the linker to inject labels.
I can get the stack location and size. But how do I get the kernel location and size? How do I do it by asking the linker or kernel loader?
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: [SOLVED] physical memory manager fails

Post by MadZarx »

Guys I found whats problem. I was overwriting the kernel in memory cuz I didn't have the kernel offset. I put a variable in a c file that linkes in the very last order and then I can get end of kernel location. Now I start the bitmap structure after the kernel :mrgreen:
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: [SOLVED] physical memory manager fails

Post by bluemoon »

you can put that variable on linker script too, search the wiki (IIRC baby steps tutorials) and consult linker manuals.
Post Reply