Kernel Paging and Linker Script

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.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Kernel Paging and Linker Script

Post by vjain20 »

Thanks a lot for the explanation! I did not think about how grub would behave.I am wondering how linux does it. AFAIK
it is linked to 0xC0000000 and loaded at1MB.
- Thanks
Vaibhav jain
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: Kernel Paging and Linker Script

Post by serviper »

. gives VMA, so you can write

Code: Select all

. = 0xC0100000
so that virtual addresses are all above 3GB. Then

Code: Select all

.text : AT(0x100000)
gives LMA at 1MB.

Also you need a prepaged page directory to map linear addresses 0~4MB and 3GB~3GB+4MB to physical address 0~4MB.
When jumping to C entry point, use an indirect jmp to force the CPU to use a virtual address.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Kernel Paging and Linker Script

Post by vjain20 »

. gives VMA, so you can write
Code:
. = 0xC0100000

so that virtual addresses are all above 3GB. Then
Code:
.text : AT(0x100000)
Thanks for the reply. I will try with different VMA and LMA
Also you need a prepaged page directory to map linear addresses 0~4MB and 3GB~3GB+4MB to physical address 0~4MB.
This means that I will have to write all the code for setting up GDT and page tables in assembly and all the addresses would be
subtracted by 0xC0000000 before paging is enabled. Am I right ?

When jumping to C entry point, use an indirect jmp to force the CPU to use a virtual address.
. Could you please explain this jumping using indirect jmp to force the
CPU to use a virtual address. I didn't understand what you mean by indirect jump.

Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Kernel Paging and Linker Script

Post by bluemoon »

vjain20 wrote:. Could you please explain this jumping using indirect jmp to force the
CPU to use a virtual address. I didn't understand what you mean by indirect jump.
CPU always see virtual address when paging is enabled(well, with a few exceptional case)
By doing that jump you jump from low address, which coincidentally identity mapped, to a higher address which virtual address differ with physical address.

For example

Code: Select all

PHYADDR=00100XXX VADDR=00100XXX EIP=00100XXX jmp ecx  (ecx = C0100XXX)
...
PHYADDR=00100XXX VADDR=C0100XXX EIP=C0100XXX execution continue here.
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: Kernel Paging and Linker Script

Post by serviper »

This means that I will have to write all the code for setting up GDT and page tables in assembly and all the addresses would be
subtracted by 0xC0000000 before paging is enabled. Am I right ?
No need to subtract all the addresses by 3GB. Actually we will enable paging immediately after the bootloader hands over control to the kernel, but before we enter the first C function we will keep using identity-mapped lower virtual addresses. Of course the initial page directory must be defined somewhere in the data section of the kernel image.
Could you please explain this jumping using indirect jmp to force the CPU to use a virtual address. I didn't understand what you mean by indirect jump.
If you write

Code: Select all

call main
to invoke main function written in C, CPU will use a signed offset to calculate address of main. So we write (in intel syntax)

Code: Select all

mov ecx, main
jmp ecx
These instructions specify register ecx from which the absolute offset from main is fetched. Thus we are now working on higher addresses instead of identity-mapped lower addresses.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Kernel Paging and Linker Script

Post by vjain20 »

Thanks for the reply! I always thought the jmp and call instructions take absolute address.
- Thanks
Vaibhav jain
Post Reply