Relative Memory Addresses

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Relative Memory Addresses

Post 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
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Relative Memory Addresses

Post 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).
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Relative Memory Addresses

Post by mark3094 »

Fantastic. That does confirm what I was thinking.

Thank you
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Relative Memory Addresses

Post 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.
Post Reply