Page 1 of 1

Linking, loading, and the higher half kernel

Posted: Mon Jan 30, 2012 11:39 pm
by duran
Afternoon osdevers,

A problem I've been having now that I've managed to get my head around interrupt servicing:

If I compile and link my kernel to be loaded to 1MiB, it is loaded at 1MiB in RAM. How then do I relocate it to the higher half of the virtual address space without having to recompute every symbol offset within? Even if I save myself some difficulty and build the image as an elf32 or similar, what is the proper method to do this? Do I need to write my own linker/loader routines already before I can move?

Or am I looking at this wrong, and all I need to do is memcpy the kernel image to the higher half and remap the page table to suit?

Re: Linking, loading, and the higher half kernel

Posted: Tue Jan 31, 2012 12:17 am
by shikhin
Hi,
duran wrote:If I compile and link my kernel to be loaded to 1MiB, it is loaded at 1MiB in RAM. How then do I relocate it to the higher half of the virtual address space without having to recompute every symbol offset within? Even if I save myself some difficulty and build the image as an elf32 or similar, what is the proper method to do this?
The simplest option would be to compile and link the kernel to be loaded at whatever higher half address you chose (let's assume 0xC0000000). Then, ask GRUB (I assume you are using GRUB) to load your kernel at the 1MiB mark. There, a position independent assembly stub could enable paging to make 0xC0000000 point to the 1MiB mark, and jump to the real kernel.

Some people also use the GDT trick, which is explained at http://wiki.osdev.org/Higher_Half_With_GDT

Regards,
Shikhin

Re: Linking, loading, and the higher half kernel

Posted: Tue Jan 31, 2012 3:05 am
by gerryg400
Another way is to use a multistage loader. The loader is starts at 1MB and it loads the kernel at the correct higher half address.