Linux AMD64 中如何使用 fs/gs 寄存器? [英] How are the fs/gs registers used in Linux AMD64?

查看:41
本文介绍了Linux AMD64 中如何使用 fs/gs 寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 x86-64 架构上,有两个寄存器有特殊用途:FS 和 GS.在 linux 2.6.* 中,FS 寄存器似乎是用来存储线程本地信息的.

  • 正确吗?
  • fs:0 中存储了什么?是否有任何描述此内容的 C 结构?
  • 那么 GS 有什么用?

解决方案

在 x86-64 中有 3 个 TLS 条目,其中两个可通过 FS 和 GS,FS 由 glibc 内部使用(在 IA32 中显然 Wine 使用FS,glibc 使用GS)..>

Glibc 使其 TLS 入口指向 struct pthread 包含一些线程的内部结构.Glibc 通常将 struct pthread 变量称为 pd,大概用于 pthread 描述符.

在 x86-64 上,struct pthreadtcbhead_t(这取决于架构,请参阅宏 TLS_DTV_AT_TPTLS_TCB_AT_TP).这个线程控制块头 AFAIU 包含一些字段,即使只有一个线程也需要这些字段.DTV 是动态线程向量,包含指向通过 dlopen() 加载的 DSO 的 TLS 块的指针.在 TCB 之前或之后,有一个静态 TLS 块,用于在(程序的)加载时链接的可执行文件和 DSO.Ulrich Drepper 的 TLS 文档(在第 3 章).

On the x86-64 architecture, two registers have a special purpose: FS and GS. In linux 2.6.*, the FS register seem to be used to store thread-local information.

  • Is that correct?
  • What is stored at fs:0? Is there any C structure that describe this content?
  • What is then the use of GS?

解决方案

In x86-64 there are 3 TLS entries, two of them accesible via FS and GS, FS is used internally by glibc (in IA32 apparently FS is used by Wine and GS by glibc).

Glibc makes its TLS entry point to a struct pthread that contains some internal structures for threading. Glibc usually refers to a struct pthread variable as pd, presumably for pthread descriptor.

On x86-64, struct pthread starts with a tcbhead_t (this depends on the architecture, see the macros TLS_DTV_AT_TP and TLS_TCB_AT_TP). This Thread Control Block Header, AFAIU, contains some fields that are needed even when there is a single thread. The DTV is the Dynamic Thread Vector, and contains pointers to TLS blocks for DSOs loaded via dlopen(). Before or after the TCB there is a static TLS block for the executable and DSOs linked at (program's) load time. The TCB and DTV are explained pretty well in Ulrich Drepper's TLS document (look for the diagrams in chapter 3).

这篇关于Linux AMD64 中如何使用 fs/gs 寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆