c - 从一个线程切换到另一个出现段错误

标签 c multithreading assembly x86-64

我正在尝试在 x86_64 中用 C 语言开发一个用户级线程库。 我有一个名为 machine_switch 的函数,它将新线程和当前线程的堆栈指针作为参数。该函数应该通过备份当前寄存器值并从新堆栈指针恢复新值来切换线程。这是我试过的。

.text
.global machine_switch

machine_switch:
    # address of the new sp is arg1
    # address of the current sp is arg2
    # need to store all required registered for old tcb
    # restore all required registred from the new tcb
    # then when you return, you should get to the new thread 

    pushq %rbx
    pushq %rbp
    pushq %r12
    pushq %r13
    pushq %r14
    pushq %r15

    movq %rdi, %rsp

    popq %r15
    popq %r14
    popq %r13
    popq %r12
    popq %rbp
    popq %rbx

    ret 

这是我用来存储线程的数据结构。

struct tcb { 
  void *sp;  /* Address of stack pointer. 
          * Keep this as first element would ease switch.S 
          * You can do something else as well. 
          */  
  int id;
  int state; /* 1 - ready state
          * 2 - running state
          * 3 - blocked state
          */
};

我有一个 tcb 列表。我在新旧 tcb 中传递 sp 作为 machine_switch 函数的参数。

但是这个函数在改变堆栈指针时给出了一个段错误。 (在 movq %rdi, %rsp)。我检查了函数的参数,它们是正确的。我错过了什么吗?

最佳答案

您的问题是您可能在调用 machine_switch 之前保存了旧的堆栈指针。但是,machine_switch 将值压入堆栈,导致保存的堆栈指针无效。要解决此问题,您可以将指针传递到要保存旧堆栈指针的位置,并在压入寄存器后存储指针:

machine_switch:
    pushq %rbx
    pushq %rbp
    pushq %r12
    pushq %r13
    pushq %r14
    pushq %r15

    movq %rsp,(%rsi) # save old stack pointer
    movq %rdi, %rsp

    popq %r15
    popq %r14
    popq %r13
    popq %r12
    popq %rbp
    popq %rbx

    ret 

关于c - 从一个线程切换到另一个出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343668/

相关文章:

linux - iptables 线程安全吗?

winapi - AMD64 jmp (FF25) 之前 REX.w 前缀的含义

c - 哪种方式为字符串保留内存?

javascript - 如何在 Emscripten 生成的代码中使用 C++ 分配的数组?

c - 从文件中读取矩阵

c - 打开时出现段错误

c++ - 在线程中访问 vector.front() 会导致运行时错误

c - 使用多线程优化硬盘读写功能的速度

linux - 关于Linux中程序的内存布局

将 C 转换为 HLA