gpt4 book ai didi

c++ - 为什么我收到错误 : interrupt service routine should have ‘unsigned long int’ as the second argument?

转载 作者:行者123 更新时间:2023-12-04 07:14:42 25 4
gpt4 key购买 nike

我收到一个简单的错误,因为我阅读了一个文档 ( https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#x86-Function-Attributes ) 并希望正确地做事。在这份文件中指出:

An interrupt handler must be declared with a mandatory pointer argument:

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame)
{
}

and you must define struct interrupt_frame as described in the processor’s manual.

Exception handlers differ from interrupt handlers because the system pushes an error code on the stack. An exception handler declaration is similar to that for an interrupt handler, but with a different mandatory function signature. The compiler arranges to pop the error code off the stack before the IRET instruction.

#ifdef __x86_64__
typedef unsigned long long int uword_t;
#else
typedef unsigned int uword_t;
#endif

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame, uword_t error_code)
{
...
}

Exception handlers should only be used for exceptions that push an error code; you should use an interrupt handler in other cases. The system will crash if the wrong kind of handler is used.


因此,推送错误代码的异常应该有 64 位长作为第二个参数。其他的应该只有用户定义的interrupt_frame 结构。因此,我尝试使用以下代码来做到这一点:
struct InterruptFrame{
UINT64 rsp;
};

//Divide by zero error
__attribute__((interrupt)) void IDT::isr0(InterruptFrame* frame) {
asm volatile("hlt");
}

//Page fault
__attribute__((interrupt)) void IDT::isr14(InterruptFrame* frame, unsigned long long int errorCode) {
asm volatile("hlt");
}
这些只是示例,因为我有更多独立的 ISR。当我编译时
g++ -static -ffreestanding -nostdlib -mgeneral-regs-only -mno-red-zone -c -m64 Kernel/Source/IDT.cpp -oKernel/Object/IDT.o
我收到一个错误说明
error: interrupt service routine should have ‘unsigned long int’ as the second argument
即使我完全按照文档中的说明做了。即使对于当前具有 unsigned long long int 作为第二个参数的页面错误处理程序,我也会收到每个 ISR 的错误。我没有在网上找到太多关于错误的搜索,所以我想在这里问。
怎么了?另外,对于 x86-64,InterruptFrame 结构应该包含哪些成员?

最佳答案

What is wrong?


我认为错误信息似乎很清楚:

error: interrupt service routine should have ‘unsigned long int’ as the second argument



根据该错误消息,我建议通过将“unsigned long int”作为第二个参数而不是 unsigned long long int 来解决问题。
错误消息的源代码揭示了使用哪种类型的条件:

gcc/config/i386/i386-options.c

  error ("interrupt service routine should have %qs "
"as the second argument",
TARGET_64BIT
? (TARGET_X32 ? "unsigned long long int"
: "unsigned long int")
: "unsigned int");

所以,似乎 unsigned int定位 32 位 x86 时应使用, unsigned long long当目标是 64 x86_64 和 unsigned long 的 32 位 ABI 时应该使用当以 x86_64 的 64 位 ABI 为目标时。

Also, what members should the InterruptFrame struct contain for x86-64?


我认为您不一定需要定义结构,直到您打算访问它。但如果你这样做,AMD64 架构
程序员手册
第 2 卷:
系统编程 说道:

8.9 Long-Mode Interrupt Control Transfers

8.9.3 Interrupt Stack Frame

Interrupt-Handler Stack


在上面,有关于内容的详细描述。
根据图表,以下似乎匹配(未经测试。我建议读者验证顺序是否正确或颠倒):
struct interrupt_frame
{
std::uint64_t instruction_pointer;
std::uint64_t code_segment;
std::uint64_t rflags;
std::uint64_t register_stack_pointer;
std::uint64_t stack_segment;
}

关于c++ - 为什么我收到错误 : interrupt service routine should have ‘unsigned long int’ as the second argument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68839642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com