Page 1 of 1

Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 8:30 am
by LPeter
Hi!
I've made my heap, so I decided to move onto running processes, however I didn't have much idea about how to do it so I looked at the brokenthorn tutorial (http://www.brokenthorn.com/Resources/OSDev24.html).
Is anyone familiar with it? The virtual address creation is not used in his code, he just gives the process the kernel's address space. So I made it to actually use the address space creation, but the kernel mapping fails.
This is my kernel mapping function, which fails (Page fault error code 2):

Code: Select all

void vmm_map_kernel_space(page_directory_t * address_space)
{
	uint32_t virtual_addr;
	uint32_t phys_addr;
	
	// User mode not set to pervent access
	int flags = PTE_FLAG_PRESENT | PTE_FLAG_RW;
	
	// Map kernel stack - don't need to, mapped with kernel
	/*vmmr_map_physical_address (address_space, 0x8000, 0x8000, flags);
	vmm_map_physical_address (address_space, 0x9000, 0x9000, flags);*/
	
	// Kernel
	virtual_addr = 0xc0000000;
	phys_addr = 0x100000;
	
	for (uint32_t i = 0; i < 32; i++)
	{
		vmm_map_physical_address(address_space, virtual_addr + (i * PAGE_SIZE), phys_addr+ (i * PAGE_SIZE), flags);
	}
	
	/*
		map display memory for debug minidriver
		idenitity mapped 0xa0000-0xBF000.
		Note:
			A better alternative is to have a driver associated
			with the physical memory range map it. This should be automatic;
			through an IO manager or driver manager.
	*/
	virtual_addr = 0xa0000;
	phys_addr = 0xa0000;
	for (uint32_t i = 0; i < 31; i++)
	{
		vmm_map_physical_address (address_space, virtual_addr + (i * PAGE_SIZE), phys_addr + (i * PAGE_SIZE), flags);
	}

	// Map page directory itself into it's address space
	vmm_map_physical_address(address_space, (uint32_t) address_space, (uint32_t) address_space, PTE_FLAG_PRESENT | PTE_FLAG_RW);
}
I hope that I provided everything that's needed and I also hope that someone can help me out.
(This problem is probably really simple but I haven't slept too much in the couple of last days, sorry for that :( )

Thank you!
Peter

Re: Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 11:21 am
by xenos
What do the faulting instruction (EIP value) and page fault address (CR2 value) tell you about the page fault?

Re: Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 12:57 pm
by LPeter
XenOS wrote:What do the faulting instruction (EIP value) and page fault address (CR2 value) tell you about the page fault?
Error code: 2
eip: 0x1004a8

Re: Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 1:02 pm
by Techel
Error code 2 means error on write -> your accessed pages are readonly.

Re: Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 2:45 pm
by LPeter
Roflo wrote:Error code 2 means error on write -> your accessed pages are readonly.
But I'm in kernel mode. Doesn't that mean, I can write readonly too?

Re: Process creation with brokenthorn tutorial

Posted: Sat Feb 28, 2015 3:00 pm
by Combuster
Intel 3A wrote:P flag (bit 0).
This flag is 0 if there is no translation for the linear address because the P flag was 0 in one of the paging-
structure entries used to translate that address.
W/R (bit 1).
If the access causing the page-fault exception was a write, this flag is 1; otherwise, it is 0. This flag
describes the access causing the page-fault exception, not the access rights specified by paging.
Therefore, the error code says page not present upon write.


In addition, there's the CR0.WP control register that dictates if writing to read-only pages causes page-faults in more privileged code. (Tip: set it. Free bug detection aid :wink:)

Re: Process creation with brokenthorn tutorial

Posted: Sun Mar 01, 2015 2:32 am
by LPeter
I just don't understand the process creation code already. Pagig is enabled, but he's still doing stuff with physical addresses...

Re: Process creation with brokenthorn tutorial

Posted: Sun Mar 01, 2015 8:56 pm
by neon
Hello,

We would like to confirm if you have modified or altered the provided demo software. The provided value of eip being 0x1004a8 is very suspect given that none of the software should be executing from that virtual address. This would be a strong indication of running arbitrary code. (eip should be >3GB in kernel or 4MB for user software.) Does the software execute without error when you do not call vmm_map_kernel_space? In addition, you should be using vmmngr_mapPhysicalAddress instead. It is simpler and less error prone. Please see the provided demo on its usage.

Please post the value of CR2 and a stack trace if possible.
Pagig is enabled, but he's still doing stuff with physical addresses...
Physical addresses never go away. They are required when working with system structures that use them; the software must be able to identify physical to virtual when needed, or use linear addresses when not. The provided software only uses physical addresses when mapping page frames into a virtual address space and identifying objects that reside at a physical location.