How to relocate test kernel's text data and bss in ram??
How to relocate test kernel's text data and bss in ram??
Hi I have small test kernel (grub as boot loader) installed gdt idt, console and serial logger. Before going to paging I want to understand how to relocated (copy all my kernel to known pages) text data and bss in ram. since after some calculation I know what is highest usable memory page bellow 4 GB space and I want my kernel data bss stack to those higher address (at the end of ram). One more thing what is the exact size of ram that DMA processor can address?
Re: How to relocate test kernel's text data and bss in ram??
alloc and memcopy or remap?
DMA16 - 16 mb
DMA32 - 4 gb or even more
DMA8 - 1 mbtushs wrote:One more thing what is the exact size of ram that DMA processor can address?
DMA16 - 16 mb
DMA32 - 4 gb or even more
If you have seen bad English in my words, tell me what's wrong, please.
Re: How to relocate test kernel's text data and bss in ram??
Sorry I did not got your ans
Point is how can I compile relocatable kernel, how to copy segments and make jump to new kernel address?
Suppose I have kernel starting and running @ 0x100000 and now I want to move it to 0x80000000 (say 2GB). I need to copy all text data and bss from old location to new. and I need to jump to new location code. set up new stack etc. But what about addresses of variable and functions in code segment which are hard coded in text section. new code will not find valid new address.alloc and memcopy or remap?
Point is how can I compile relocatable kernel, how to copy segments and make jump to new kernel address?
Re: How to relocate test kernel's text data and bss in ram??
There're only two ways.
1) Put init code into section(s) with 1 mb precompiled starting address and put relocatable code into section(s) with 2 gb precompiled starting address.
2) On the same principles relocate relocatable code dinamically using fixup info.
Edited. There's one hard way.
3) Use position independent relocatable code.
I use memcopy method.
1) Put init code into section(s) with 1 mb precompiled starting address and put relocatable code into section(s) with 2 gb precompiled starting address.
2) On the same principles relocate relocatable code dinamically using fixup info.
Edited. There's one hard way.
3) Use position independent relocatable code.
It is compiler specific. You can put two binaries into one kernel binary by some tools too. In fasm I just use org directive.Point is how can I compile relocatable kernel, how to copy segments and make jump to new kernel address?
Code: Select all
org 8000h
put startupcode16,initcode16,startupdata16,initdata16
virtual
rb 10000h-$
end virtual
use32
put startupcode32,initcode32,startupdata32,initdata32
align 4
getsum32 STARTUP_SUM32
label PACKAGE_BASE
org SYMBOLS_BASE <<<--- LOOK HERE
put code,data
getsum32 SYMBOLS_SUM32
virtual
put bss
set KERNEL_PCOUNT,($+0FFFh)/1000h-KERNEL_PINDEX
put rma
set KERNEL_TCOUNT,($+3FFFFFh)/400000h-KERNEL_TINDEX
end virtual
set SYMBOLS_SIZE,$-SYMBOLS_BASE
org PACKAGE_BASE+SYMBOLS_SIZE
rb 3FFh - ($+7) mod 400h
dd (-STARTUP_SUM32-SYMBOLS_SUM32) and 0FFFFFFFFh,0AA554745h
Code: Select all
mov esi,PACKAGE_BASE
mov edi,SYMBOLS_BASE
mov ecx,(SYMBOLS_SIZE+3)/4
rep movsd
jmp KCODE:@f
@@:
...
jmp start ; start is label within relocated code
If you have seen bad English in my words, tell me what's wrong, please.
Re: How to relocate test kernel's text data and bss in ram??
Can u point out some example, link of 2nd option, how to build relocatable code with gcc and what fixup info is required.
I want to decide relocation address after loading be'cos then I will have all physical memory info and depending on size of RAM I will load kernel to upper address. I am using c , gcc, gas assembler.
I want to decide relocation address after loading be'cos then I will have all physical memory info and depending on size of RAM I will load kernel to upper address. I am using c , gcc, gas assembler.
- Combuster
- 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: How to relocate test kernel's text data and bss in ram??
Building a relocating linker is not the biggest deal, the bigger problem is that you need the relocation data for that (which means module/ramdisk support very early in the kernel, or some other complex magic to copy raw ELF data into the binary), and that you can't sanely transfer anything that's live. Worst of it all, it is in almost all cases completely unnecessary, and I get the idea that you want to do something more complex than the final result as an intermediate step.Before going to paging I want to understand how to relocate
If you want a kernel at 2G, link it there and load it there. If you want a kernel at 2G even if there's no memory there which is likely for an emulator, link it at 2G, load it somewhere else, and then use paging. Only if your kernel should be at a non-fixed address, does relocating or PIC make sense, but it also implies that everything else in your system should later be relocatable.
Re: How to relocate test kernel's text data and bss in ram??
What is reason to do that? If you use paging your kernel can be placed/mapped at any fixed precompiled virtual address. As Combuster said you should make relocation using programming way only if your kernel will be able to be placed/mapped at different virtual addresses.tushs wrote:I want to decide relocation address after loading be'cos then I will have all physical memory info and depending on size of RAM I will load kernel to upper address.
If you have seen bad English in my words, tell me what's wrong, please.
Re: How to relocate test kernel's text data and bss in ram??
why I got into relocation is I want set lower memory free for DMA use. I wanted to avoid kernel in that address space.
- LegendDairy
- Member
- Posts: 52
- Joined: Sat Nov 06, 2010 10:42 am
- Location: Antwerp (Belgium)
Re: How to relocate test kernel's text data and bss in ram??
You could also use an intrd to load a binairy, then you relocated that binairy, then jump to that binairy and that one erases the orignal kernel, but this will take some precious start up time...
Re: How to relocate test kernel's text data and bss in ram??
I use memcopy for that reason to make available base memory for legacy hardware. You must understand that only virtual addresses are important. It's easy to write init code in asm (position independent or not) that will copy your kernel at any physical address and then will map it at fixed precompiled virtual address or firstly will map free memory at this virtual address and then will copy your kernel. There's small problem when your kernel is loaded by GRUB because it can try to load the kernel at high virtual address (not at low physical address) but ELF stores both virtual and physical addresses and I think that will be alright.tushs wrote:why I got into relocation is I want set lower memory free for DMA use. I wanted to avoid kernel in that address space.
If you have seen bad English in my words, tell me what's wrong, please.
- Combuster
- 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: How to relocate test kernel's text data and bss in ram??
Then why not do the most straightforward thing and just tell grub to load your kernel at 16M? Unless you plan on running on an underpowered 586 or worse, that pretty much solves your problem instantly.tushs wrote:why I got into relocation is I want set lower memory free for DMA use. I wanted to avoid kernel in that address space.