Page 1 of 1

Relative Memory Addresses

Posted: Fri May 11, 2012 10:54 pm
by mark3094
Hi,

A quick question about how the kernel sees memory.

I have a custom written boot loader, which loads an executable (the kernel, written in C) to 0x100000. The boot loader is responsible for entering protected mode, and setting up the GDT.

I'm looking at getting the boot loader to get some information from interrupts before entering protected mode. For example, the memory map. I would then like to get the Kernel to read this from memory (I'll figure that part out later).

As the Kernel knows it's running at 0x100000, does it see memory addresses as relative to that position or not? For example, if I store my memory map at 50:3500 in real mode, can the kernel just read from 0x3a00 in protected mode?).

Thank you

Re: Relative Memory Addresses

Posted: Sat May 12, 2012 12:51 am
by iansjack
If I understand your question correctly, the answer is no. You need to set ds to a segment selector and addressing is then relative to that segment. The address that the program is running at is irrelevant (being determined by the cs register).

True relative addressing (relative to the instruction pointer) is only available in long mode making it trivial to relocate programs within a flat address space (and, effectively, doing away with segment selectors).

Re: Relative Memory Addresses

Posted: Sat May 12, 2012 6:21 am
by mark3094
Fantastic. That does confirm what I was thinking.

Thank you

Re: Relative Memory Addresses

Posted: Sat May 12, 2012 6:43 am
by bluemoon
mark3094 wrote:if I store my memory map at 50:3500 in real mode, can the kernel just read from 0x3a00 in protected mode?).
There are multiple way to read memory from physical address 0x3a00. I think you have mixed them up.

The kernel may:
1. access it with absolute address (ie. have the full address not related to anything), things like char*p=(char*)0x3A00;
2. access it with relative address (eg. related to RIP), things like mov rdi, rip-0xC600, where rdi ends up = 0x3A00, and 0xC600 is what we called relative offset

Then, you have segment and paging that map such address into physical region of memory.
For example, my kernel read the physical address at 0000:0600 with address FFFFFFFF:80000600.