Page 1 of 1

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

Posted: Tue Feb 04, 2003 11:26 pm
by Perica
..

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

Posted: Wed Feb 05, 2003 1:27 am
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.

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

Posted: Wed Feb 05, 2003 5:21 am
by Perica
..

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

Posted: Wed Feb 05, 2003 6:11 am
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 ...)

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

Posted: Wed Feb 05, 2003 6:22 am
by Perica
..

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

Posted: Wed Feb 05, 2003 6:31 am
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 ...