Skip to contentSkip to conversation

elf loader: use the virtual address

RFC PATCH In review Aurelien Jarno 4 participants 6 messages Jan 9, 2009 – Jan 10, 2009

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 elf loader: use the virtual address

Most Linux kernels have their physical address corresponding to they virtual address in the ELF header. This is however not true anymore for the PowerPC kernels (>= 2.6.25).

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

As all the other kernels/bios I have looked have the same virtual and physical address, I don't think it will break other targets. This is what is done in the patch below. Alternatively, we can add a new argument to the load_elf functions, to select between virtual and physical load address.

Any opinon?

elf_ops.h +1 -1
index feea12f..19d1a5e 100644
@@ -231,7 +231,7 @@ static int glue(load_elf, SZ)(int fd, int64_t address_offset,
             }
             /* address_offset is hack for kernel images that are
                linked at the wrong physical address.  */
-            addr = ph->p_paddr + address_offset;
+            addr = ph->p_vaddr + address_offset;
 
             cpu_physical_memory_write_rom(addr, data, mem_size);
 
Signature · 4 lines
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
#1 lore raw
Quoted 8 lines · Aurelien Jarno

Most Linux kernels have their physical address corresponding to they virtual address in the ELF header. This is however not true anymore for the PowerPC kernels (>= 2.6.25).

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

Quoted 7 lines

As all the other kernels/bios I have looked have the same virtual and physical address, I don't think it will break other targets. This is what is done in the patch below. Alternatively, we can add a new argument to the load_elf functions, to select between virtual and physical load address.

Any opinon?

I wonder if some platforms don't use the address_offset parameter to simulate that behaviour.

Anyway wouldn't a parameter be cleaner and allow more flexibility?

Laurent

Quoted 21 lines
elf_ops.h +1 -2
index feea12f..19d1a5e 100644
@@ -231,7 +231,7 @@ static int glue(load_elf, SZ)(int fd, int64_t address_offset,
            }
            /* address_offset is hack for kernel images that are
               linked at the wrong physical address.  */
-            addr = ph->p_paddr + address_offset;
+            addr = ph->p_vaddr + address_offset;
 
            cpu_physical_memory_write_rom(addr, data, mem_size);
 
--
 .''`.  Aurelien Jarno             | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   [email protected]         | [email protected]
  `-    people.debian.org/~aurel32 | www.aurel32.net
#2 lore raw
Quoted 15 lines · Laurent Desnogues
Quoted 14 lines · Aurelien Jarno

Most Linux kernels have their physical address corresponding to they virtual address in the ELF header. This is however not true anymore for the PowerPC kernels (>= 2.6.25).

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

As all the other kernels/bios I have looked have the same virtual and physical address, I don't think it will break other targets. This is what is done in the patch below. Alternatively, we can add a new argument to the load_elf functions, to select between virtual and physical load address.

Quoted 4 lines
Quoted 1 line

Any opinon?

I wonder if some platforms don't use the address_offset parameter to simulate that behaviour.

PowerPC already does that. The offset is relative to the load address, so it should still be read correctly.

Quoted 2 lines

Anyway wouldn't a parameter be cleaner and allow more flexibility?

Maybe that's what I am going to implement.

Signature · 4 lines
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
#3 lore raw
Quoted 3 lines

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

This sounds wrong.

Surely we should be loading it at the paddr with translation disabled, hat's the whole point of having both paddr and vaddr in the ELF headers. If the ppc kernel isn't setting p_paddr to the load address then what is it setting it to?

Quoted 7 lines

/* address_offset is hack for kernel images that are
linked at the wrong physical address. */

elf_ops.h +1 -1
-            addr = ph->p_paddr + address_offset;
+            addr = ph->p_vaddr + address_offset;

As all the other kernels/bios I have looked have the same virtual and physical address, I don't think it will break other targets.

This will definitely break things. In particular bare metal applications built for the Luminary Micro boards (and probably any other system that uses XIP from flash). I also have several non-linux OS and bootloaders that rely on the current elf loader behavior.
It's standard practice to use the paddr when loading an image. This si consistent with gdb and several boot loaders.

See also http://lists.gnu.org/archive/html/qemu-devel/2008-10/msg00864.html

Paul

#4 lore raw
Quoted 5 lines · Paul Brook
Quoted 3 lines

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

This sounds wrong.

I agree. It sounds to me like if powerpc linux changed their linking to produce elf files with correct physical addresses and that makes it hard for qemu to support both the new and the old images.

Maybe ppc could use the low/highaddr arguments to find out if the kernel objects really needs to use hte -0xc00000000 offset or no?

my guess is that older kernels need that offset but that the newer ones dont.

#5 lore raw
In reply to Paul Brook
Quoted 10 lines · Paul Brook
Quoted 3 lines

For PowerPC, the kernel needs to be started with address translation enabled (that's even true for the firmware), and thus the kernel loaded at the virtual address.

This sounds wrong.

Surely we should be loading it at the paddr with translation disabled, hat's the whole point of having both paddr and vaddr in the ELF headers. If the ppc kernel isn't setting p_paddr to the load address then what is it setting it to?

It was set to the same value as the virtual address (0xc0000000), and it is now set to 0x00000000.

Quoted 8 lines
Quoted 7 lines

/* address_offset is hack for kernel images that are
linked at the wrong physical address. */

elf_ops.h +1 -1
-            addr = ph->p_paddr + address_offset;
+            addr = ph->p_vaddr + address_offset;

As all the other kernels/bios I have looked have the same virtual and physical address, I don't think it will break other targets.

Quoted 7 lines

This will definitely break things. In particular bare metal applications built for the Luminary Micro boards (and probably any other system that uses XIP from flash). I also have several non-linux OS and bootloaders that rely on the current elf loader behavior.
It's standard practice to use the paddr when loading an image. This si consistent with gdb and several boot loaders.

If we want to mimic bootloaders, both quik and yaboot are using p_vaddr.

Signature · 4 lines
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net