What does %fs:0xfffffffffffffff8 mean in asm_adm64.s

591 views
Skip to first unread message

Jason Young

unread,
May 20, 2016, 8:30:10 AM5/20/16
to golang-nuts
Hi, guys. I was learning go internal recently by using gdb. And got sucked In runtime.mcall(which located in src/runtime/asm_adm64.s) 

By debugging runtime.Gosched. mcall was executed and then gosched_m. 
I am curious about where did the arguments gp *g coming from. And finally I found out it was being processed in mcall()

 
 
func Gosched() {
mcall(gosched_m)
}
 
// Gosched continuation on g0.
func gosched_m(gp *g) {
if trace.enabled {
traceGoSched()
}
goschedImpl(gp)
}

TEXT runtime·mcall(SB), NOSPLIT, $0-8
MOVQ fn+0(FP), DI

get_tls(CX)
MOVQ g(CX), AX // save state in g->sched $rax now contains the address of (gp *g)
 
 
   │0x453430 <runtime.mcall>        mov    0x8(%rsp),%rdi                                                                                                                          │
   │0x453435 <runtime.mcall+5>      mov    %fs:0xfffffffffffffff8,%rax          //$rax now contains address of gp                                             │
   │0x45343e <runtime.mcall+14>     mov    (%rsp),%rbx

I got confused about the %fs:0xfffffffffffffff8, %rax. What does $fs point to ? and why 0xfffffffffffffff8 here.

Many thanks

Ian Lance Taylor

unread,
May 20, 2016, 10:00:21 AM5/20/16
to Jason Young, golang-nuts
On Fri, May 20, 2016 at 12:26 AM, Jason Young
<red.wolf....@gmail.com> wrote:
>
> I got confused about the %fs:0xfffffffffffffff8, %rax. What does $fs point
> to ? and why 0xfffffffffffffff8 here.

%fs is an x86 segment register. It's used on ELF based systems as a
pointer to a thread local memory area. On x86 Go stores the current
goroutine pointer in the thread local memory area, so that it is
always easily available. The offset of -8 is simply the offset into
the thread local area where the goroutine pointer is stored. The
instructions you are looking at are generated by the get_tls macro in
the source; tls in this case is an abbreviation for thread local
storage.

Ian

Jason Young

unread,
May 20, 2016, 10:37:55 AM5/20/16
to golang-nuts, red.wolf....@gmail.com
Oh,  thank you very much lan, That's a very helpful explanation But why it won't show as `mov   0x8(%fs),%rax`
I've checked the AT&T syntax manual here did not found any example like this one.
Thanks again!

Ian Lance Taylor

unread,
May 20, 2016, 12:25:49 PM5/20/16
to Jason Young, golang-nuts
On Fri, May 20, 2016 at 7:37 AM, Jason Young
<red.wolf....@gmail.com> wrote:
> Oh, thank you very much lan, That's a very helpful explanation But why it
> won't show as `mov 0x8(%fs),%rax`
> I've checked the AT&T syntax manual here did not found any example like this
> one.

%fs:8 is the traditional way to disassemble the use of a segment
register. Segment registers are not normal registers and don't hold
normal values. Your link doesn't mention segment registers at all.
For details you should dig into the Intel architecture manuals; for
example: http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf

Ian

Jason Young

unread,
May 22, 2016, 11:43:44 PM5/22/16
to golang-nuts, red.wolf....@gmail.com
Problem solved Many Thanks!
Reply all
Reply to author
Forward
0 new messages