[solved] Freebsd ASSYM macro: help to understand ?

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
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

[solved] Freebsd ASSYM macro: help to understand ?

Post by JulienDarc »

Hello,

Reading a lot on osdev and xv6 helped me to understand the majority of what is going on in an OS.
I decided to experiment and modify the freebsd code.

I just came across that code :
(freebsd-master/sys/amd64/amd64/cpu_switch.S)

Code: Select all

	movq	%r15,PCB_R15(%r8)
And the Macros :
(freebsd-master/sys/sys/assym.h)

Code: Select all

#define ASSYM_BIAS              0x10000 /* avoid zero-length arrays */
#define ASSYM_ABS(value)        ((value) < 0 ? -((value) + 1) + 1ULL : (value))

#define ASSYM(name, value)                                                    \
char name ## sign[((value) < 0 ? 1 : 0) + ASSYM_BIAS];                        \
char name ## w0[(ASSYM_ABS(value) & 0xFFFFU) + ASSYM_BIAS];                   \
char name ## w1[((ASSYM_ABS(value) & 0xFFFF0000UL) >> 16) + ASSYM_BIAS];      \
char name ## w2[((ASSYM_ABS(value) & 0xFFFF00000000ULL) >> 32) + ASSYM_BIAS]; \
char name ## w3[((ASSYM_ABS(value) & 0xFFFF000000000000ULL) >> 48) + ASSYM_BIAS]
and
(freebsd-master/sys/amd64/amd64/genassym.c)

Code: Select all

ASSYM(PCB_R15, offsetof(struct pcb, pcb_r15));
Ok so, PCB_R15 is the offset of pcb_r15 inside the struct pcb.


Why do we have a parameter (%r8) since I don't see where it fits in the macro ?
There is obviously something done under the bonnet that I don't get..

Could you please help ?

Thanks

Julien
Last edited by JulienDarc on Tue May 05, 2015 6:38 am, edited 2 times in total.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Freebsd ASSYM macro: help to understand ?

Post by alexfru »

I think PCB_R15 in the assembly code refers to a global variable and the parens mean a memory access and not a macro with an argument. You probably need to refresh on the AT&T assembly instruction/operand syntax.

I also don't think you're looking at the right macro. That macro seems to define a bunch of global variables using C syntax for variable declarations, which I doubt the assembler can understand.
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

Re: Freebsd ASSYM macro: help to understand ?

Post by JulienDarc »

Yes I became rusty with the addressing..

So here is my interpretation :

PCB_R15 is the offset and %r8 is what we are looking at (pcb struct for instance).

And the whole become readable by a compiler because it is the offset inside the struct.

How could I forget about that..

Thanks alexfru for putting me back on track :)
Post Reply