Kernel location

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.
albeva
Member
Member
Posts: 42
Joined: Thu Aug 21, 2008 8:31 pm

Re: Kernel location

Post by albeva »

I went with common trend and placed my kernel to 3GB point as well. Although this does effectievly limit the memory my apps will be able to use to 4GB - if I map the kernel into every virtual space. Although it could help with perfomance I guess.
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Re: Kernel location

Post by Virtlink »

Most kernels get loaded at physical address 0x100000 (1 MiB), and some are mapped to a higher address (say 0xC0000000) using paging. But physically, the kernel is still at 0x100000 in memory. Doesn't this interfere with DMA, which uses the lower 16 MiB? Do you physically copy the kernel somewhere else? And what about other structures which are below 0x100000 (thinking about BIOS things and the text video memory)?

- Virtlink
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Kernel location

Post by CodeCat »

I doubt that a kernel is more than a few MB, so unless you have a kernel that fills up all available DMA memory, I don't think it'll be a problem.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Kernel location

Post by Combuster »

Also, you still have the memory below 1M for DMA purposes
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Kernel location

Post by quok »

I've recently changed my kernel from being able to be booted from GRUB directly to using a separate stub loader. The stub loader is currently invoked by GRUB (although I have grand plans of making it a complete bootmanager sometime in the future). This stub loader takes care of translating multiboot information into a format that is used by my kernel. Currently the kernel is passed as a GRUB module, but in the future the stub loader will be able to read the kernel from disk.

Anyway, after loading the kernel (and parsing the ELF program headers), the loader will enable paging and exec the kernel. A future version will be able to enter long mode and handle ELF64 files as well. Because the kernel is entered in to with paging already enabled, I was able to change my linker script so the kernel so the LMA and VMA are both 3GB instead of the more traditional 1MB/3GB. I also changed the linker script to always emit relocations, and altered the stub loader to be able to relocate the kernel at boot time.

I had to make a couple of other changes to my linker script to make sure any variables declared there where relative to their sections and not absolute for all of this to work, but so far it's working beautifully.

The plan is to use this functionality in the future to be able to load and exec a new kernel without having to do a complete reboot, but I haven't gotten near that far yet.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Kernel location

Post by Brendan »

Hi,
CodeCat wrote:I doubt that a kernel is more than a few MB, so unless you have a kernel that fills up all available DMA memory, I don't think it'll be a problem.
The problem would be when there isn't much RAM free (e.g. normal processes are using most of it). For example, there might be 2 MiB of RAM free and you might want to allocate 64 KiB of contiguous physical pages for ISA DMA, but you might not be able to do this because of free space fragmentation. To help to minimize this problem you'd want to use pages above 0x01000000 if you can, to increase the chance that enough pages below 0x01000000 will be free in case you need them.

If your device drivers allocate DMA buffers once when they start (instead of dynamically allocating, freeing and resizing buffers as needed, while the OS is running); and if your OS doesn't allow device drivers to be installed/restarted/replaced without rebooting (including all forms of "hot-plug", even "standard non-USB" floppy drives in laptop docking stations); then it probably won't matter much. However, these are nice features to have...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Re: Kernel location

Post by Virtlink »

Brendan wrote:If your device drivers allocate DMA buffers once when they start (instead of dynamically allocating, freeing and resizing buffers as needed, while the OS is running); and if your OS doesn't allow device drivers to be installed/restarted/replaced without rebooting (including all forms of "hot-plug", even "standard non-USB" floppy drives in laptop docking stations); then it probably won't matter much. However, these are nice features to have...
And when I would want these features (which I would :) ) then I'll have to move the kernel out of the way. Are there other structures below physical 1 MiB which need to be saved and restored, or may never be touched? The video memory at 0xB8000 can't be used. Or are these areas not part of the Grub memory map, so I could wipe all memory which Grub says is available?

The ISA DMA article says about this:
Apart from making sure that you have a copy somewhere of the information in the first 4K of memory and the Extended BIOS Data Area (these are for VM86 tasks if you decide to add BIOS/DOS/VESA/PCI support later), the above example will allow you to have a flat paged memory area from 0 to forever while still allowing you OS to use old DMA dependent peripherals.
Thanks for any help,

- Virtlink
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Kernel location

Post by Brendan »

Hi,
Virtlink wrote:And when I would want these features (which I would :) ) then I'll have to move the kernel out of the way. Are there other structures below physical 1 MiB which need to be saved and restored, or may never be touched? The video memory at 0xB8000 can't be used. Or are these areas not part of the Grub memory map, so I could wipe all memory which Grub says is available?
GRUB normally gets it's memory map from the BIOS (Int 0x15, eax=0xE820), and you can wipe all memory which is reported as available RAM. However, if you're planning on using the BIOS's environment (e.g. returning to real mode or using virtual80x86 mode) then you shouldn't wipe the first 4 KiB of available RAM (the BIOS's "BIOS Data Area", which includes the BIOS's "Interrupt Vector Table", etc). Also, older computers may not support the "get memory map" BIOS function (Int 0x15, eax=0xE820) - in this case GRUB won't give you a memory map, and will only give you the amount of available RAM below 1 MiB and the amount of available RAM between 1 MiB and 16 MiB (I think).
Virtlink wrote:The ISA DMA article says about this:
Apart from making sure that you have a copy somewhere of the information in the first 4K of memory and the Extended BIOS Data Area (these are for VM86 tasks if you decide to add BIOS/DOS/VESA/PCI support later), the above example will allow you to have a flat paged memory area from 0 to forever while still allowing you OS to use old DMA dependent peripherals.
The EBDA is delicate - you should never touch it (including copying it somewhere else and then using the RAM where the EBDA was). The reason for this is that modern computers use SMM (System Management Mode) while the OS is running, and the BIOSs SMM code may use data in the EBDA. If you copy the EBDA somewhere else and then put your data there, the BIOS may trash your data and might crash because it doesn't know it's SMM data was shifted.

Also, RAM between 0x01000000 and 0xFFFFFFFF is slightly more useful than ram above 4 GiB, because in some cases you can't use a page above 4 GiB (e.g. 32-bit PCI devices, and the Page Directory Pointer Table if you use PAE). This gives you 3 pools of RAM with different levels of usefulness, where pages above 4 GiB are the least useful and should be used whenever possible; pages between 0x01000000 and 0xFFFFFFFF are slightly more useful and should be used if there aren't any pages above 4 GiB (unless you need a page with a 32-bit physical address); and pages between 0x00000000 and 0x00FFFFFF are the most useful (as they can be used for any purpose) and shouldn't be used unless there's no free pages in any other area (unless you need a page with a 20-bit physical address).

Further, a good OS will try to make sure that there isn't many free pages - a free page is a page that isn't being used to improve performance (file system caches, etc). This might mean trying to make sure that about 512 KiB is free below 0x00FFFFFF, and that 256 KiB if free between 0x01000000 and 0xFFFFFFFF, and that nothing is free above 4 GiB.

Some people boot their kernel and then setup paging. This isn't necessarily a good idea. Instead, you could have a second stage or something that detects memory and sets up paging; so that paging is already enabled before the kernel is started. This provides a lot more flexibility - for example, one of the things I do is keep track of faulty RAM, so if the page at 0x00101000 (for e.g.) is faulty then the boot code will make sure it's not used, and the OS will still boot reliably. Another thing I do is NUMA optimizations - for e.g. have a different copy of the kernel's code for each NUMA domain, so that all CPUs can always get the kernel's code from "close/fast" memory, rather than having an unbalanced system where some CPUs perform worse than others because they have to get kernel code from "distant/slow" memory.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Re: Kernel location

Post by Virtlink »

Brendan, thanks for the very useful information!
Brendan wrote:Instead, you could have a second stage or something that detects memory and sets up paging; so that paging is already enabled before the kernel is started. This provides a lot more flexibility - for example, one of the things I do is keep track of faulty RAM, so if the page at 0x00101000 (for e.g.) is faulty then the boot code will make sure it's not used, and the OS will still boot reliably.
I'll be going that way them. I figured that it also makes life easier booting a 64-bit kernel, or a higher-half kernel.

- Virtlink
Post Reply