GCC Inline KernelGS Offset

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
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

GCC Inline KernelGS Offset

Post by tsdnz »

Edit: Found it out, thanks!

Hi All, I am trying to get GCC to produce:

Code: Select all

movq %%gs:0x45, xxxx
The 0x45 above will change as my structure changes, but I am having trouble telling asm how to fix the value.
Any help is appreciated.

Code: Select all

#define GetKernelGSPointerData(KernelPointerType, Member, ReturnType, ReturnVariableName) \
	ReturnType ReturnVariableName; \
	{ int ofs = offset(KernelPointerType, Member);  asm volatile ("movq %%gs:%1, %0" : "=q"(ReturnVariableName) : "m"(ofs) ); }

Code: Select all

#define offset(type, member) ((WORD)(QWORD)&((type *)0)->member);
Example calling code:

Code: Select all

GetKernelGSPointerData(tCPU, Sleeping, QWORD, Test);
It currently Produces:

Code: Select all

  30c2cb:	c7 04 24 00 00 00 00 	mov    DWORD PTR [rsp],0x0
  30c2d2:	65 48 8b 34 24       	mov    rsi,QWORD PTR gs:[rsp]
Which is fine, but I would like a

Code: Select all

mov rsi, QWORD PTR gs:0
Last edited by tsdnz on Tue Jul 14, 2015 8:37 pm, edited 1 time in total.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: GCC Inline KernelGS Offset

Post by tsdnz »

If I change to:

Code: Select all

#define GetKernelGSPointerData(KernelPointerType, Member, ReturnType, ReturnVariableName) \
	ReturnType ReturnVariableName; \
	{ int ofs = offset(KernelPointerType, Member);  asm volatile ("movq %%gs:(%1), %0" : "=q"(ReturnVariableName) : "r"(ofs) ); }
This is produced, which is fine, but still wondering how to produce gs:0x0

Code: Select all

  30c2cb:	31 f6                	xor    esi,esi
  30c2cd:	65 67 4c 8b 06       	mov    r8,QWORD PTR gs:[esi]
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: GCC Inline KernelGS Offset

Post by tsdnz »

Figured it out, as usual, just after posting here. LOL

Code: Select all

#define GetKernelGSPointerData(KernelPointerType, Member, ReturnType, ReturnVariableName) \
	ReturnType ReturnVariableName; \
	{ asm volatile ("movq %%gs:%1, %0" : "=q"(ReturnVariableName) : "m"(*(QWORD*)offset(KernelPointerType, Member)) ); }

Code: Select all

#define offset(type, member) ((QWORD)&((type *)0)->member)
Found this: http://f.osdev.org/viewtopic.php?f=13&t=21721
Post Reply