linux x86 64 中 MSR_GS_BASE 的详细信息 [英] Detail about MSR_GS_BASE in linux x86 64

查看:14
本文介绍了linux x86 64 中 MSR_GS_BASE 的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出 Linux 内核中 MACRO current 的详细信息.当前的最终汇编代码为:

I tried to figure out the details of MACRO current in Linux kernel. The final assembly code of current is:

movq %%gs:0xb000,%0

上面的代码可以工作!但是当我打印 %%gs 时,它的值为 0,所以 %%gs 指向 GDT NULL 的第一项!!??它是如何工作的?

The code above can work! But when I print the %%gs, its value is 0, so the %%gs points to the first item of GDT NULL!!?? How it works?

mov %%gs, %0

相反,gs 的基数在 MSR_GS_BASE 中,当前可以像这样替换:

Instead, the base of gs is in MSR_GS_BASE, and the current can be replaced like:

/*0xb000 is the offset of per_cpu__current_task*/
cur_task = (unsigned long*)(x86_rdmsr64(MSR_GS_BASE) + 0xb000);
println("cur_task:%p",*cur_task);

我的问题是:

%gs 指向 GDT NULL 的第一项!!??从 MSR_GS_BASE 读取它是如何工作的,它是 CPU 特性吗?我需要一些关于此的参考.

%gs points to the first item of GDT NULL!!?? How it works as read from MSR_GS_BASE, is it a CPU feature? I need some references about this.

推荐答案

来自 AMD 架构程序员手册第 2 卷:系统编程,第 4.5.3 节:

From the AMD Architecture Programmer's Manual Volume 2: System Programming, section 4.5.3:

64 位模式下的 FS 和 GS 寄存器. 与 CS、DS、ES 和 SS 段不同,FS 和 GS 段覆盖可用于 64 位模式.在 64 位模式下使用 FS 和 GS 段覆盖时,它们各自的基地址用于有效地址 (EA) 计算.完整的 EA 计算然后变成 (FS 或 GS​​).base + base + (scale * index) + 位移.FS.base 和 GS.base 值也扩展到完整的 64 位虚拟地址大小,如图 4-5 所示.允许生成的 EA 计算涵盖正地址和负地址.

FS and GS Registers in 64-Bit Mode. Unlike the CS,DS,ES, and SS segments, the FS and GS segment overrides can be used in 64-bit mode. When FS and GS segment overrides are used in 64-bit mode, their respective base addresses are used in the effective-address (EA) calculation. The complete EA calculation then becomes (FS or GS).base + base + (scale * index) + displacement. The FS.base and GS.base values are also expanded to the full 64-bit virtual-address size, as shown in Figure 4-5. The resulting EA calculation is allowed to wrap across positive and negative addresses.

[...]

有两种方法可以更新 FS.base 和 GS.base 隐藏描述符字段的内容.第一个专供特权软件使用(CPL = 0).FS.base 和 GS.base 隐藏描述符寄存器字段映射到 MSR.特权软件可以使用单个 WRMSR 指令将规范形式的 64 位基地址加载到 FS.base 或 GS​​.base 中.FS.base MSR 地址为 C000_0100h,GS.base MSR 地址为 C000_0101h.

There are two methods to update the contents of the FS.base and GS.base hidden descriptor fields. The first is available exclusively to privileged software (CPL = 0). The FS.base and GS.base hidden descriptor-register fields are mapped to MSRs. Privileged software can load a 64-bit base address in canonical form into FS.base or GS.base using a single WRMSR instruction. The FS.base MSR address is C000_0100h while the GS.base MSR address is C000_0101h.

更新 FS 和 GS 基本字段的第二种方法适用于在任何权限级别运行的软件(当实现支持并通过设置 CR4[FSGSBASE] 启用时).WRFSBASE 和 WRGSBASE 指令分别将 GPR 的内容复制到 FS.base 和 GS.base 字段.当操作数大小为 32 位时,清除基址的高位双字.WRFSBASE 和 WRGSBASE 仅在 64 位模式下支持.

The second method of updating the FS and GS base fields is available to software running at any privilege level (when supported by the implementation and enabled by setting CR4[FSGSBASE]). The WRFSBASE and WRGSBASE instructions copy the contents of a GPR to the FS.base and GS.base fields respectively. When the operand size is 32 bits, the upper doubleword of the base is cleared. WRFSBASE and WRGSBASE are only supported in 64-bit mode.

这篇关于linux x86 64 中 MSR_GS_BASE 的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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