声明“extern struct cpu * cpu asm("%gs:0");”是什么意思? [英] What does the declaration“extern struct cpu *cpu asm("%gs:0");” mean?

查看:365
本文介绍了声明“extern struct cpu * cpu asm("%gs:0");”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读xv6源代码时,我对以下声明的语法感到困惑。任何人都可以解释给我吗?

pre $ c $ extern struct cpu cpu asm(%gs:0);


解决方案

extern struct cpu * cpu 表示。你的问题是: asm(%gs:0)部分是什么意思?



代码使用称为 asm标签的gcc扩展来说变量 cpu 由汇编器字符串%gs:0 定义。



这不是打算如何使用此扩展程序,而是被视为破解



关于gs(和fs)的精彩讨论 here ,但总之gs指向当前线程的本地存储。 gs中的数据格式取决于您的操作系统(Windows与Linux非常不同)。这个特定的代码是说在来自gs的偏移量 0 中,有一个指向 struct cpu 的指针。


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");

解决方案

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.

这篇关于声明“extern struct cpu * cpu asm("%gs:0");”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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