Page 1 of 1

how do i find a data physical address in PMode

Posted: Tue Jul 20, 2004 9:51 pm
by chen17981
Hi everyone

Now I have entered Pmode, not enable PAGE, I want to know how to get a data linear address and physical address.

USE Nasm compiler
For example:
Already in Pmode

[BITS 32]

Var dd lable1

;some code

lable1:

; some code

how do I find the physical address of lable1 and write the address to Var ?

Thank you in advance.

Re:how do i find a data physical address in PMode

Posted: Tue Jul 20, 2004 10:31 pm
by bkilgore
NASM:

Code: Select all

call get_phys_address_offset
get_phys_address_offset:
pop eax
sub eax, get_phys_address_offset

;eax now has the value you need to add to any linked address to get the physical address

mov ebx, lable1
add ebx, eax
mov dword [var], ebx


Something like that I think will work. Even though you dont have paging enabled, because you're asking this question I assume your code was probably linked to a different address than it was loaded (so hopefully all of your code up to this point has been location independent).

By doing a near call and then popping the return address, we can get the physical address of the call and then subtract off the linked, virtual address to get the virtual-to-physical offset. Then you can use that to reference any virtual address by just adding it first. If all you need is the values of that one label, you can just do

Code: Select all

call lable1
lable1:
pop eax
mov dword [var], eax
I think that should work, although I do more in AT&T syntax now so i might be a little off in the syntax.

Re:how do i find a data physical address in PMode

Posted: Wed Jul 21, 2004 1:34 am
by chen17981
Hi bkilgore, thank you for your help

Your code gave me some ideas to deal with the problem i met in my os project. But unfortunately it still did not work, maybe
my expression is unclear.

I just want to know how to get a data physical address,

for example, the physical address of lable1 is needed.
Var dd lable1

lable1:
db 0
db 0
lable1_end:

You offered me a method for getting proc address in code section. But I have a question why eax should be popped, I check CALL instruction the nasm manual, and find nothing is related with EAX.

Could you give me your answer? Thank you in advance.

Re:how do i find a data physical address in PMode

Posted: Wed Jul 21, 2004 10:19 am
by bkilgore
When you do a near call, it pushes eip onto the stack. Before paging is enabled, this eip will be the physical address to return to after the call, which (because the label immediately follows the call) is the physical address of the label. By popping that back into eax, we now have to physical address of the label in eax. It would be easier if we could just say 'mov eax, eip' but you can't do that.

Now that we have the physical address of that one place, we can do a couple of different things. If you just want that one physical address, you're all done, its in eax. If you want the physical address of other things, we can calculate the offset from the linked address to the physical address by subtracting the linked address of the label. Then we have an offset that we can add to any linked address (including that label, any other label, any other variable, etc) to get the physical address.

That just reminded me of one thing. You can't move a value directly into your 'var' because that will be resolved to its linked address, so a command like

Code: Select all

mov [var], eax
Will end up looking something like

Code: Select all

mov [0xc000023d], eax
Or whatever the linked address of the variable is. To actually access this variable before enabling paging and mapping the linked address to the physical address, you need to use that offset we found whenever dealing with any non-relative code (such as variable and label locations), so what you really want is:

Code: Select all

mov [var + eax], eax
Which will end up looking something like

Code: Select all

mov [0xc000023d + 0x40000000], eax

or

mov [0x10023d], eax
Assuming that your linked address was 0xc0000000 and the kernel was loaded at 0x100000


i hope this makes it a little clearer...

- Brandon

Re:how do i find a data physical address in PMode

Posted: Wed Jul 21, 2004 7:21 pm
by chen17981
Thank you ,bkilgore, you are a nice guy.