Process creation with brokenthorn tutorial

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
LPeter
Member
Member
Posts: 30
Joined: Wed Jan 28, 2015 7:41 am

Process creation with brokenthorn tutorial

Post 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
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Process creation with brokenthorn tutorial

Post by xenos »

What do the faulting instruction (EIP value) and page fault address (CR2 value) tell you about the page fault?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
LPeter
Member
Member
Posts: 30
Joined: Wed Jan 28, 2015 7:41 am

Re: Process creation with brokenthorn tutorial

Post 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
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: Process creation with brokenthorn tutorial

Post by Techel »

Error code 2 means error on write -> your accessed pages are readonly.
LPeter
Member
Member
Posts: 30
Joined: Wed Jan 28, 2015 7:41 am

Re: Process creation with brokenthorn tutorial

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Process creation with brokenthorn tutorial

Post 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:)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
LPeter
Member
Member
Posts: 30
Joined: Wed Jan 28, 2015 7:41 am

Re: Process creation with brokenthorn tutorial

Post by LPeter »

I just don't understand the process creation code already. Pagig is enabled, but he's still doing stuff with physical addresses...
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Process creation with brokenthorn tutorial

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply