Passing a C/C++ Pointer to a Different Segment Register (ES/

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Passing a C/C++ Pointer to a Different Segment Register (ES/

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:48 pm, edited 2 times in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Passing a C/C++ Pointer to a Different Segment Register (

Post by Pype.Clicker »

Perica Senjak wrote: Hey,

What i want to do is Pass a C/C++ Pointer using a Different Segment Register other than "DS";

Can this be accomplished easily in C/C++??
It can be accomplished through a "farptr" structure you have to declare that will look like

Code: Select all

struct farptr {
    void *offset;
    selector_t segment;
}
The problem is that the compiler will not let you do *farX = 1 in C, but in C++, overloading every operator for the far class, maybe you could handle it ...
Also, when paging maps a Certain Area Of Memory - Is it Possible to Acess the Memory from Both Addresses: The Pages Address and the Normal One (If you know what i mean)??
I'm afraid i don't see what you mean, but i guess you wonder if you can access memory by its virtual address *and* with its physical address. In that case the answer is no.

The reason is every memory reference will go through the paging chip once paging is enabled (bit 31 in cR0 set) --> if you want to access physical frame #x with paging enabled, you have to
- find out a free page and map it to #x,
- flush the TLB cache for #x,
- do your access,
- unmap the page,
- flush the TLB for #x again.

This is what Clicker's Page Frame Viewer (code here) is about ...

Cya.
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Passing a C/C++ Pointer to a Different Segment Register (

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:48 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Passing a C/C++ Pointer to a Different Segment Register (

Post by Pype.Clicker »

I'll write a Linked Assembler Function that Simply writes a Specified Byte/Word/dWord/qWord of Data to a Specified Register, and a Specified Selector:Offset - Something like this: bool WriteFS_dWord(unsigned int Selector, unsigned int Offset, unsigned short Data);
eeek! i would not do it that way, if i were you ... because it will mean that every byte access will require a function call that the processor will never be able to optimize.

i'd rather declare WriteFs_dword(...) as a __inline__ function holding one inlined-assembly command. That way, the processor can optimize the generation of the offset and the register that will receive the result ...

Also i would opt for setFS(selector) getFS() that will alter a specific segment descriptor and assume the selector is set in writeFS_dword(offset,value) -- so that you don't have to do some mov <segment>,<selector> everytime (which would slow down your code because of re-loading of the GDT/LDT content at every memory access ...)
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Passing a C/C++ Pointer to a Different Segment Register (

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:49 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Passing a C/C++ Pointer to a Different Segment Register (

Post by Pype.Clicker »

from info gcc :


The alternate keywords `__asm__', `__extension__', `__inline__'
and `__typeof__' continue to work despite `-ansi'. You would not
want to use them in an ANSI C program, of course, but it is useful
to put them in header files that might be included in compilations
done with `-ansi'. Alternate predefined macros such as `__unix__'
and `__vax__' are also available, with or without `-ansi'.
The main reason i use them is simply to remind me of their presence and of the evil fact they'll be inlined only when optimizations are turned on ...
Post Reply