Page 1 of 2

Kernel location

Posted: Mon Aug 25, 2008 8:04 am
by albeva
Some kernels say they load themselves to 2GB point. Why is that. Is there a practical reason for this?

Re: Kernel location

Posted: Mon Aug 25, 2008 9:03 am
by itisiuk
this is so that the kernel does not mess with the mem below 16mb as it is used for dma, vm86 and other stuff.
however it is up to up where u want to put you kernel in memory.

you should read up on the higher half tutorials on the wiki. also i recommend reading up on dma and the floppy drivers.

Re: Kernel location

Posted: Mon Aug 25, 2008 1:31 pm
by AJ
itisiuk wrote:this is so that the kernel does not mess with the mem below 16mb as it is used for dma, vm86 and other stuff.
If you plan to use paging, that really doesn't matter unless you intend to identity map your kernel.

One of the main reasons is that many kernels map the kernel in to each application's memory space. This means that if an application makes a system call, that system call can be handled in the current memory context without needing a potentially expensive memory space switch.

There is no reason why you can't have the kernel at a low address and applications in the higher half using this system, but this would require your app programmers to link their programs to somewhere like 0x80000000.

Cheers,
Adam

Re: Kernel location

Posted: Mon Aug 25, 2008 11:34 pm
by egos
AJ wrote:There is no reason why you can't have the kernel at a low address and applications in the higher half using this system, but this would require your app programmers to link their programs to somewhere like 0x80000000.
There's one reason why kernel places at high address. It's simplest way to change proportions between app space and kernel space for statically compiled applications.

Re: Kernel location

Posted: Tue Aug 26, 2008 12:23 am
by Stevo14
AJ wrote: There is no reason why you can't have the kernel at a low address and applications in the higher half using this system, but this would require your app programmers to link their programs to somewhere like 0x80000000.
Which really isn't an issue if you have all of your developers use a standard linker script (I believe this is recommended for several reasons in the article about porting newlib). Then the programmer won't need to care where his/her program is linked and the kernel developers have some control over the locations of programs relative to the kernel.

egos wrote:There's one reason why kernel places at high address. It's simplest way to change proportions between app space and kernel space for statically compiled applications.
I'm not sure If I fully understand what you mean. If you have application programmers statically link programs to 0x80000000 then the kernel developers still have the freedom to put the kernel at either 0x100000 mark or somewhere higher like 0xC0000000 or 0xF0000000 (as long as the statically compiled binary is not extraordinarily large of course). Again, standard linker script to the rescue. :)

Re: Kernel location

Posted: Tue Aug 26, 2008 1:43 am
by egos
Stevo14 wrote:I'm not sure If I fully understand what you mean. If you have application programmers statically link programs to 0x80000000 then the kernel developers still have the freedom to put the kernel at either 0x100000 mark or somewhere higher like 0xC0000000 or 0xF0000000 (as long as the statically compiled binary is not extraordinarily large of course).
If kernel places at low address and there is a need to make kernel space larger or smaller, it's necessary to recompile all application binaries with new image base. If kernel places at high address then applications don't need recompiling.

Re: Kernel location

Posted: Tue Aug 26, 2008 1:53 am
by Combuster
If you look at it from the other side:
a kernel at 0xC000000 will have one GB reserved for itself by default.
a kernel at 0x100000 might only reserve what it needs, leaving more space free for application's use.
and you can always compile a binary to a sufficiently high address so that you will never have problems expanding the kernel area, i.e. for a lower half kernel, compile to at least 1GB.

Or compile applications with relocations enabled like I do :)

Re: Kernel location

Posted: Tue Aug 26, 2008 2:03 am
by AJ
Combuster wrote:and you can always compile a binary to a sufficiently high address so that you will never have problems expanding the kernel area, i.e. for a lower half kernel, compile to at least 1GB.
If something in the future means that you want to change the amount of virtual address space belonging to the kernel, this system means that every app, not just the kernel itself needs a recompile (assuming position dependent code). Not likely, but still may as well take this in to account in the design phase.

Cheers,
Adam

Re: Kernel location

Posted: Tue Aug 26, 2008 3:46 am
by Stevo14
egos wrote: If kernel places at low address and there is a need to make kernel space larger or smaller, it's necessary to recompile all application binaries with new image base. If kernel places at high address then applications don't need recompiling.
Unless the applications were compiled in the first place to be as far away from the kernel as possible. Like this:

Code: Select all

     <-- The kernel is as far this ways as possible.
                      Applications are as far this way as possible. -->
|--------------------------------- Memory --------------------------------|
It doesn't matter if it's a higher half kernel or a lower half kernel. The idea is the same.

But I like what Combuster pointed out: position independent code fixes all of these problems.

Re: Kernel location

Posted: Tue Aug 26, 2008 5:04 am
by egos
Stevo14 wrote:position independent code fixes all of these problems.
Yes, but not relocations because it descent performance.

Re: Kernel location

Posted: Tue Aug 26, 2008 1:36 pm
by Combuster
but not relocations because it descent performance.
Position independent code impacts performance since it must recompute all offsets to a known base every other instruction at runtime.
Relocatable code impacts performance since it must recompute all offsets to a known base at load time only.

Re: Kernel location

Posted: Tue Aug 26, 2008 2:43 pm
by egos
Combuster wrote:it must recompute all offsets to a known base at load time only.
And exactly this descends performance. Why to relocate code when it is possible not to make this :)

Re: Kernel location

Posted: Tue Aug 26, 2008 3:22 pm
by Stevo14
egos wrote:
Combuster wrote:it must recompute all offsets to a known base at load time only.
And exactly this descends performance. Why to relocate code when it is possible not to make this :)
Because, for a small cost (fractions of a second each time a binary is loaded), you can have very large benefits such as being able to control at runtime where a binary is located, seamless changes in the amount and location of reserved kernel space, greater flexibility overall etc.

Re: Kernel location

Posted: Tue Aug 26, 2008 11:47 pm
by egos
Stevo14 wrote:Because, for a small cost (fractions of a second each time a binary is loaded), you can have very large benefits such as being able to control at runtime where a binary is located, seamless changes in the amount and location of reserved kernel space, greater flexibility overall etc.
I'm continuing... great problems with mapping directly from executable files and so on :)

Re: Kernel location

Posted: Wed Aug 27, 2008 8:30 am
by AndrewAPrice
I load my kernel at 3GB. This is because in the future if I decide I want to give my applications more memory, or my kernel more memory, I can move my kernel up/down (it's actually a C #define that sets my kernel's entry address, and another value in the linker script), and my applications can just load from 0x0 (in practice they load from 1MB, but I'm going to change it to 0, so there's no chance of pointers pointing to 0, since a lot of [my] code assume's that's unallocated) regardless of where I place my kernel in the future.