Memory Remapping

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.
pskyboy

Memory Remapping

Post by pskyboy »

Hey Guys

When i load my kernel it gets loaded to the 1Mb mark and then the boot loader jumps to this. Because of this i link all my stuff from 1Mb up in the linker file. My question is my OS is using 8Mb of physical memory from 0-8Mb and when i enable paging i want to remap this to 3Gb. How do i do this, at the moment i have to map the bottom 8Mb as linear and present to stop my kernel crashing. I am guessing this is because the kernel is linked to jump to address around the 1Mb mark.

So do i need to link my kernel to the 3Gb mark and then set up paging before i jump to the kernel?

Peter
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Memory Remapping

Post by distantvoices »

you have to link your kernel to virtual 3 gb, BUT you have to subtract the 3 gb from all your adresses until paging is enabled. After this point, no more adress mangling is needed. To achieve this, you can do a simple paging enabling stuff prior to the jump to your kernel, which maps the first 8 mb to 3 gb. In your kernel init stuff, you can initialize more complex page tables and page directories.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
pskyboy

Re:Memory Remapping

Post by pskyboy »

Cheers

Thats what i have started doing, i was just wondering if there was another maybe nicer way to go about it but i suspected there wouldn't be :).

Peter
Tim

Re:Memory Remapping

Post by Tim »

You can use the GDT to subtract the 3GB from your addresses, by setting the base address to (0000_0000 - C000_0000) = 4000_0000.
pskyboy

Re:Memory Remapping

Post by pskyboy »

That sounds intersting. So what would i do link the kernel to the 3Gb mark and then set the GDT to 3Gb when im booting?
Tim

Re:Memory Remapping

Post by Tim »

I think I described this in my memory management 1 tutorial. In short, you link the kernel to 3GB, load it anywhere, and jump to it. Then you need to set up the GDT with the special base address using mangled pointers (i.e. subtracting 3GB and adding LOAD_ADDR to each pointer). Once you LGDT with the new GDT, you can use global variables as normal.

However, until you enable paging, you will need to access normal memory (e.g. B8000) using either reverse-mangled pointers or another data segment (whose base address is zero).
pskyboy

Re:Memory Remapping

Post by pskyboy »

cheers, i seem to have missed that bit in your Tutorial. I guess this will only work on a x86 processor which could be a bit of a problem as i want my OS to be portable.

Peter
Tim

Re:Memory Remapping

Post by Tim »

Yes, but the whole startup sequence is non-portable anyway, so it doesn't matter.
pskyboy

Re:Memory Remapping

Post by pskyboy »

Thats, true!

So now i have set my GDT to a value of 410000000 which will give me 1Mb when i jump to 3Gb. I am having trouble though i keep getting triple faults. I recalculated the address of both the jump to clear the prefetch and the GDT location and these seem to be right when i disasemble the code, but it still triple faults.

Any Ideas
Peter
pskyboy

Re:Memory Remapping

Post by pskyboy »

What do you do about the GDT location. Becasue is this is in the bootloader which is at 0x7C00 so thsi will be at 0x7C00 + a bit. SO what happens when you enable the GDT with the 0x40100000 selector in it can no longer access the GDT table a chicken egg situation.

Any Help greatly appreciated

Peter
Tim

Re:Memory Remapping

Post by Tim »

As I said earlier:
However, until you enable paging, you will need to access normal memory (e.g. B8000) using either reverse-mangled pointers or another data segment (whose base address is zero).
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Memory Remapping

Post by distantvoices »

Would an entry in the page directory pointing to a pagetable mapping the physical adresses to themselves - identity mapping 1:1 - do the job too?

else I would do it with some adress juggling (subtracting 3 gb constant from the adresses to get the physical adresses or similar...) Tim brings it better to a point then me.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
pskyboy

Re:Memory Remapping

Post by pskyboy »

Yeah i have tried subtracting 3Gb from the address but it just crashes and burns at the moment. It is throwing up running in bogus memory on bochs

Peter
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Memory Remapping

Post by Pype.Clicker »

probably something wrong in your paging setup ... bochs doesn't like executing code that is in inexistent memory ...
pskyboy

Re:Memory Remapping

Post by pskyboy »

Okay i have traced it through in bochs and it seems the IDTR is not loading properly.

It should be loading as

Code: Select all

limit = 0x1f,  base = 0xBFF07C9A
It loads as

Code: Select all

limit = 0x1f,  base = 0xF07C9A
Has anyone got any ideas why this is happening, could this be a bug in bochs?

Peter
Post Reply