包含在内联汇编中
我们正在开发一个玩具操作系统作为课堂作业。 我在编写内核恐慌函数时遇到了一些麻烦。
它应该保存所有寄存器,调用一些类似 printf 的函数,然后打印保存的寄存器并停止 cpu。现在它被定义为一个宏:
#define panic(...) \
do{ \
asm volatile("SAVE_REGISTERS %1\n\t" : "m="(_panic_context)); \
_panic_printk(&_panic_context, __VA_ARGS__); \
while(0)
_panic_context
是一个全局变量,包含线程的保存寄存器和其他一些东西。问题出在 SAVE_REGISTERS
上。它是在汇编器头文件中某处定义的宏,但我不知道如何包含它。文件中的简单 #include 显然不起作用。我尝试过谷歌搜索并编写有趣而绝望的东西(例如汇编器字符串中的#include :-)),但没有任何帮助。您有什么想法如何解决这个问题吗?
我们使用 GCC 并为 MIPS 进行编译(在模拟器中运行:-))
编辑: SAVE_REGISTERS
是用 .macro SAVE_REGISTERS
... 定义的。它不能出现在 C 宏中,因为它在其他汇编模块中使用。 我无法用它创建 .S 文件,因为恐慌必须是可变的。或者至少我想不出任何其他方法来做到这一点。
We are working on a toy operating system as a assignment for a class.
I'm having some trouble with writing of the kernel panic function.
It should save all registers, call some printf-like function, then print the saved registers and halt the cpu. Right now it's defined as a macro:
#define panic(...) \
do{ \
asm volatile("SAVE_REGISTERS %1\n\t" : "m="(_panic_context)); \
_panic_printk(&_panic_context, __VA_ARGS__); \
while(0)
_panic_context
is a global variable that contains saved registers of a thread and some more stuff. The problem is with SAVE_REGISTERS
. It is a macro defined somewhere in an assembler header file, but I don't know how to include it. Simple #include in the file obviously doesn't work. I've tried googling and writing funny and desperate stuff (like #include in the assembler strings :-) ) but nothing helped. Do you have any ideas how to solve this?
We're using GCC and compile for MIPS (running in a simulator :-) )
edit:SAVE_REGISTERS
is defined with .macro SAVE_REGISTERS
... . It can't be in a C macro, because it's used in other assembly modules.
I can't make a .S file with it, because panic has to be variadic. Or at least I couldn't come up with any other way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您误解了宏的工作原理。它们在任何其他编译发生之前被扩展。您不能使用
asm
发出一些包含宏的代码,因为该宏永远不会被扩展。相反,您可以尝试将
SAVE_REGISTERS
定义为 C 宏,它使用asm
语句来构建汇编代码:然后您可以这样做
I think you're misunderstanding how macros work. They get expanded before any other compiling happens. You can't use
asm
to emit some code which contains a macro because that macro will never be expanded.Instead you might try defining
SAVE_REGISTERS
as a C macro which uses theasm
statement to build your assembly code:then you can do
我没有 MIPS 系统,但在我的 x86 loonix 机器上我尝试了
#include
并发现#
是注释字符,所以我尝试了.include
当我引用文件名时,它就起作用了:.include "something.i"
。现在,如果您有头文件和用于汇编代码的有组织的基础结构,那么我必须想知道为什么您首先要使用内联汇编来执行此操作。为什么不直接制作一个真正的
.s
或.S
文件,包含汇编器头文件,并使其成为系统中的一流模块?我见过用 asm 完成的小型杰作,即使在
Makefile
中已经有locore.S
的项目中,想想看......I don't have a MIPS system lying around, but on my x86 loonix box I tried
#include
and discovered that#
is the comment character, so I tried.include
and that worked as soon as I quoted the file name:.include "something.i"
.Now, if you have header files and an organized infrastructure for assembler code, I have to wonder why you want to do this with inline asm in the first place, though. Why not just make a real
.s
or.S
file, include the assembler header file, and make it a first-class module in your system?I've seen small masterpieces done with asm even in projects that already had a
locore.S
in theirMakefile
, go figure...