mengning / linuxkernel

操作系统导论教学资料、《庖丁解牛Linux内核》配套资料
https://j.youzan.com/pfzVI9
GNU General Public License v3.0
263 stars 101 forks source link

time-asm不成功,运行显示段错误 #3

Open zyq5428 opened 5 years ago

zyq5428 commented 5 years ago

下面是我照着写的代码:

include

include

int main(void) { time_t tt; struct tm *t;

    // tt = time(NULL);

    __asm__ volatile(
        "mov $0, %%ebx\n\t"
        "mov $0xd, %%eax\n\t"
        "int $0x80\n\t"
        "mov %%eax, %0\n\t"
        :"=m"(tt)
    );

    t = localtime(&tt);

    printf("time: %d:%d:%d:%d:%d:%d:\n", t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);

    return 0;

}

我用的系统是32位的debian,其他几个内嵌汇编的代码都正常运行,唯独这个运行错误。

hero@debian:~/kernel_analysis/code/chapter4$ vim time-asm.c hero@debian:~/kernel_analysis/code/chapter4$ gcc -m32 -o time-asm time-asm.c hero@debian:~/kernel_analysis/code/chapter4$ ./time-asm Segmentation fault

mengning commented 5 years ago

没看出来哪里指针用错了,使用time函数方式运行正常的话,有可能你的系统的time系统调用编号不是0xd

zyq5428 commented 5 years ago

那从哪里可以查debian 9 32位的系统函数调用编号呢?系统调用向量是下面的

define IA32_SYSCALL_VECTOR 0x80

ifdef CONFIG_X86_32

define SYSCALL_VECTOR 0x80

endif

mengning commented 5 years ago

0x80是中断向量,不是系统调用编号 应该看你的内核版本,然后找对应版本的内核源代码 比如32位x86 3.18.6内核的系统调用列表http://codelab.shiyanlou.com/xref/linux-3.18.6/arch/x86/syscalls/syscall_32.tbl

zyq5428 commented 5 years ago

我查了,我用的4.9.0,在./arch/x86/entry/syscalls/syscall_32.tbl目录下显示位 13 i386 time sys_time compat_sys_time

mengning commented 5 years ago

好像不是这个问题,那再仔细检查代码看哪里有疏漏造成指针错误