为何执行push指令触发invalid opcode异常?(OS开发)
当前操作与可行案例
我基于C语言开发32位玩具内核已有一段时间,近期在尝试执行动态定义的代码,以便后续添加二进制加载器。
目前我的做法是创建包含指令操作码的char数组,将其复制到新的可执行页起始位置,并追加ret指令的操作码,示例如下:
char div_by_zero [] = { 0xba, 0x00, 0x00, 0x00, 0x00, // mov 0x0, edx 0xb8, 0xfa, 0x00, 0x00, 0x00, // mov 0xfa, eax 0xb9, 0x00, 0x00, 0x00, 0x00, // mov 0x0, ecx 0xf7, 0xf1 // div ecx ---> DIV BY 0 }; void exec_test(){ char* thing = (char*) get_virtual_address(0,1, 0); char postamble [] = {0xc3}; // ret char *code = div_by_zero; /* [...] omitted: copying to 'thing' and adding the 'postamble' at the end... */ __asm__("call %0" :: "m" (thing)); // Starts executing the "string" (lol) }
这是我第一个测试案例,如预期触发了DIVISION BY ZERO异常。
随后我测试了以下代码:
// movb $0x41,0x1000 char write_A_on_1000 [] = { 0xc6, 0x05, 0x00, 0x10, 0x00, 0x00, 0x41 };
打印内存地址0x1000处的字符,结果如预期为大写A。
问题描述
我尝试了另一个看似简单的案例:
char push_one [] = {0x6a, 0x01};
该代码本应将整数1压入栈中,但与其他案例不同,它触发了INVALID OPCODE异常。
我曾尝试手动实现push操作(如将栈指针减4等),但同样触发INVALID OPCODE异常。由于后续的加载器需要正常执行push指令,我必须解决此问题。
(注:目标平台为i386机器,当前使用QEMU运行内核。)
内容的提问来源于stack exchange,提问作者mell_o_tron
相关产品推荐
相关产品推荐

