Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.8 getg实现的一个疑惑 #536

Open
leewaiho opened this issue Apr 21, 2021 · 8 comments
Open

3.8 getg实现的一个疑惑 #536

leewaiho opened this issue Apr 21, 2021 · 8 comments

Comments

@leewaiho
Copy link

leewaiho commented Apr 21, 2021

提示:哪一章节的问题,建议如何修改
image
有点不太能理解

0(CX)(TLS*1) 是如何与 (TLS) 等价的

不知能否解答一下疑惑, 谢谢

按照我个人的理解
CX被赋值TLS
0(CX)(TLS*1) 是否应该被解释为 (TLS*1 + TLS +0)(TLS*2)
为什么会等价于(TLS)

@chai2010
Copy link
Owner

TLS属于线程局部空间,对于不同的线程是不同的。有些CPU禁止直接读取TLS的内容到寄存器,
因此需要先获取TLS地址到寄存器,再通过寻址的方式读取。

简单来说 MOVQ (TLS), CX 就是读取 TLS 表示地址指向的内存的数据,对应C语言的二级指针 CX = **TLS
要在一条汇编指令实现C语言的二级指针读取肯定是比较困难的,因此就有了前面的2个指令写法:

MOVQ TLS, AX
MOVQ 0(AX)(TLS*1), CX // load g into CX

0(AX)(TLS*1)其实是0(AX),后面的(TLS*1)属于一个注解信息,
告诉link要针对不同的线程处理不同的偏移量(每个线程的TLS和和线程编号有相关性)。

(TLS*1)这种注解写法其实是有一定的歧义风险的,但是因为Go语言值需要用TLS保存一个g指针所有不会出现冲突。
说到底,Go的汇编语言是一种高级编译语言,还需要进行一次编译才能到底层机器的汇编。

具体的细节在这里有说明:
https://github.com/golang/go/blob/master/src/cmd/internal/obj/x86/obj6.go#L72

@leewaiho
Copy link
Author

leewaiho commented Apr 21, 2021

感谢柴大解惑

如果把0(AX)(TLS*1) 当做 0(AX)看待就能理解了。

但是这里还有两个疑惑:

  1. 编译器?(或者link) 是如何解决0(AX)(TLS*1)与golang asm 表达式上的冲突的呢?
    image
  2. 为什么需要(TLS*1)这个注解呢? MOVQ TLS, AX 不足以说明 AX寄存器中的值存储的是TLS的地址这层关系吗?

截图来自: https://golang.org/doc/asm#x86

@chai2010
Copy link
Owner

0(AX)(TLS*1)是合法的汇编语法,但是Go汇编不会出现(TLS*1)。因此语法虽然没有错误,但是非法的语义。将某些符合语法但是语义非法的代码作为特殊的用途是常见的技巧(比如有人在浮点数的Nan里面隐藏一些标志信息),区分是没有问题的。

MOVQ TLS, AX是一个独立的指令,这会需要引入上下文的信息。不是不行,而是涉及上下文会把汇编器的代码搞的更复杂。当然,如果这个工具是其他人写的,或许又是不同的解法。

另外,如果省略(TLS*1)就会变成MOVQ 0(AX), CX,这可能不是一个正确的寻找语法(间接寻址需要2个寄存器)。

@leewaiho
Copy link
Author

leewaiho commented Apr 22, 2021

感谢柴大解惑

将某些符合语法但是语义非法的代码作为特殊的用途是常见的技巧(比如有人在浮点数的Nan里面隐藏一些标志信息),区分是没有问题的。

是否可以理解为 (TLS*1) 之于 golang asm 相当于 NaN之于浮点数?

IEEE 754-1985中,用指数部分全为1、小数部分非零表示NaN。
FROM: https://zh.wikipedia.org/wiki/NaN

即在处理这一块的时候会先判断是否是特殊用例 例如寄存器名称为TLS时进行特殊处理,否则才按照默认规则处理?


另外似乎即使省略了(TLS*1)之后,MOVQ 0(AX), CX 应该也还是一个合法的寻址语句,在这里有相应的应用,亦或者是说我理解错柴大想表达的意思了?

@cch123
Copy link
Collaborator

cch123 commented Apr 25, 2021

这个在不同平台翻译出来的结果不一样,其实理解成一一个特殊的宏就行了

@leewaiho
Copy link
Author

leewaiho commented Apr 26, 2021

似乎在这里找到了答案

@byronic-azure
Copy link

Hello,

We have an exciting opportunity for you! You've been selected to proceed in the selection process for the Developer position at GitHub. Congratulations on your achievement!

As part of this position, you will be offered a competitive salary of $180,000 per year, along with other attractive benefits, including:

  • Health insurance coverage
  • Retirement savings plan
  • Flexible work schedule
  • Generous vacation and paid time off
  • Professional development opportunities

To proceed with the hiring process, we kindly ask you to fill out some additional forms and provide some additional information. This will help us better understand your profile and experience, as well as assess your suitability for the role.

Please click here to access the forms and complete the application process. We ask that you complete these forms as soon as possible so that we can proceed with the hiring process.

Important: You have 24 hours to complete the application process.

If you have any questions or need further information, please don't hesitate to contact us.

Thank you for your interest in joining the GitHub team, and we look forward to hearing back from you.

Best regards,
GitHub Recruitment Team
Eeyore741, @manipulator01, @wangkang, @gzw13999, @bitrocks, @MilleniumSpark, @davidzhuwei, @anysoft, @johnhoangsoftware, @orf53975, @aronalrasyid, @gobanza, @0x2a94b5, @asessa, @mishafrenkel, @dorokhin, @valeamoris, @kennylbj, @UB1AFU, @yfge

@changkaiyan
Copy link

changkaiyan commented Feb 20, 2024 via email

@github-staff github-staff deleted a comment Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants