user9730905
user9730905

Reputation: 41

Why physical address of Aarch64 kernel image is nonnegative?

I'm recently learning about booting system of Linux kernel. (v4.6, with ARM64 arch.)

In the source code arch/arm64/kernel/head.S, definition of __PHYS_OFFSET is:

#define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)

where KERNEL_START is simply defined to be _text section.

And if I'm right, TEXT_OFFSET is a random number determined during kernel compile, as /arch/arm64/Makefile says:

TEXT_OFFSET := $(shell awk 'BEGIN {srand(); printf "0x%03x000\n", int(512 * rand())}')

so that the kernel image file has random location, as the linker script /arch/arm64/kernel/vmlinux.lds.S includes:

. = KIMAGE_VADDR + TEXT_OFFSET; 
.head.text : {                          
    _text = .;
    HEAD_TEXT
}

Here, KIMAGE_VADDR is a virtual address 0xFFFF000000000000 + 128M. Since TEXT_OFFSET is added, section _text will be randomly located.

Rest parts of head.S map KIMAGE_VADDR to __PHYS_OFFSET to enable MMU.

My question is this: is __PHYS_OFFSET = _text - TEXT_OFFSET always nonnegative?

I don't know where would be exact physical location of _text, but I think 512 * rand() might be as big as 512 * 32767 ~ 10MB.

Do I make sense? Is there any reason makes these codes safe?

Upvotes: 2

Views: 841

Answers (1)

Barmar
Barmar

Reputation: 782168

vmlinux.lds.S does:

. = KIMAGE_VADDR + TEXT_OFFSET; 

followed by

_text = .;

So _text = KIMAGE_VADDR + TEXT_OFFSET. When you then subtract TEXT_OFFSET, __PHYS_OFFSET will be the same as KIMAGE_VADDR.

Thus, if KIMAGE_VADDR is non-negative, so is __PHYS_OFFSET.

Upvotes: 1

Related Questions