loading flat binary 32bits application

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
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

loading flat binary 32bits application

Post by digo_rp »

I´m trying to load and exec a flat binary file compiled in gcc with nasm

I link that file into 0x0000000

and at my kernel I load into a position physical e.g 0x131000

now I have that question. how do I setup my page_table and page_dir for

that new process ?

I need to setup the first page_entrie for page table right ?

but how do I get the right value and put on it ?

like page_table[0] = (((0x131000 >> 12) & 0x3ff) << 12);

?

that calc doesn´t works

and on ip value should I put " 0 " for starting ?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

If I understand you correctly, and you want a separate per process page directory, you need to do the following:

1. Create and zero a new page directory (0x1000 aligned) somewhere.
2. Create and zero a new page table - pass the physical address of this and any flags to Pade_Dir[0], eg:

Code: Select all

PD[0] = pt | PAGE_PRESENT | PAGE_WRITE | PAGE_USER;
3. Allocate physical RAM for your new process and pass this address to your first PTE in the same way. In your case, this will be:

Code: Select all

PT[0] = 0x131000 | PAGE_PRESENT | PAGE_WRITE | PAGE_USER;
4. I would advise loading one of the 4MB ranges in your PD as itself. For example:

Code: Select all

PD[0x3FF] = physical_pd_address | PAGE_PRESENT | PAGE_WRITE;
This will allow the easy addition of later mappings in to this address space.

5. When you are ready to switch address space, load CR3 with the physical page directory location.

If you do not want separate process spaces, you would do the same but ignore step 1.

I'm not quite sure what all the bit shifting is about in your code, but I may be missing something.

Cheers,
Adam
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Code: Select all

unsigned int *page_table = (unsigned int *)page_dir[0] & 0xFFF;
page_table[0] = physical_address | 0x1 /* present */ | 0x2 /* writeable */ | 0x4 /* user-mode */;
This assumes:

* The executable is < 4KB in size.
* physical_address is set to the physical position you load your file to (e.g. 0x131000).

EDIT: Damn yer eyes AJ! Posted < 1 minute before me! :evil:
Post Reply