Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

gcc - What does the declaration“extern struct cpu *cpu asm("%gs:0");” mean?

When I'm reading the xv6 source code, I'm confused about the syntax of the declaration below. Can anyone explain it to me?

extern struct cpu *cpu asm("%gs:0");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume you understand what extern struct cpu *cpu means. The question you have is: What does the asm("%gs:0") part mean?

This code is using a gcc extension called asm labels to say that the variable cpu is defined by the assembler string %gs:0.

This is NOT how this extension is intended to be used and is considered a hack.

There's an excellent discussion of gs (and fs) here, but in short gs points to the current thread's local storage. The format of the data at gs depends on your OS (Windows is very different than Linux). This particular code is saying that at offset 0 from gs, there is a pointer to a struct cpu.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...