【问题标题】:Why the linux read ip register from rcx register in the entry_SYSCALL_64 function?为什么linux从entry_SYSCALL_64函数中的rcx寄存器读取ip寄存器?
【发布时间】:2018-03-19 08:19:40
【问题描述】:

我正在研究linux中的系统调用处理过程。

我发现在用户进程运行syscall指令调用系统调用时调用了entry_SYSCALL_64函数。

这个函数保存中断帧。

但是,当它推送 ip 中断帧时,它读取的不是 rip 而是 rcx。

这段代码很糟糕。

ENTRY(entry_SYSCALL_64)
  UNWIND_HINT_EMPTY
  /*
   * Interrupts are off on entry.
   * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
   * it is too small to ever cause noticeable irq latency.
   */

  swapgs
  /*
   * This path is only taken when PAGE_TABLE_ISOLATION is disabled so it
   * is not required to switch CR3.
   */
  movq  %rsp, PER_CPU_VAR(rsp_scratch)
  movq  PER_CPU_VAR(cpu_current_top_of_stack), %rsp

  /* Construct struct pt_regs on stack */
  pushq $__USER_DS      /* pt_regs->ss */
  pushq PER_CPU_VAR(rsp_scratch)  /* pt_regs->sp */
  pushq %r11        /* pt_regs->flags */
  pushq $__USER_CS      /* pt_regs->cs */
  pushq %rcx        /* pt_regs->ip ********************************** */
GLOBAL(entry_SYSCALL_64_after_hwframe)
  pushq %rax        /* pt_regs->orig_ax */

  PUSH_AND_CLEAR_REGS rax=$-ENOSYS

  TRACE_IRQS_OFF

  /* IRQs are off. */
  movq  %rsp, %rdi
  call  do_syscall_64   /* returns with IRQs disabled */
  ....

我在重要的一行输入了这么多的“*”。

在评论中,它说它保存了ip寄存器并且这个偏移量是正确的ip偏移量。

但是,它读取的是 rcx....

有人知道为什么吗?

【问题讨论】:

    标签: assembly linux-kernel operating-system system-calls cpu-registers


    【解决方案1】:

    因为the syscall instruction在进入内核之前将syscall之后的指令地址存储到RCX中。

    【讨论】:

    • 非常感谢!就是这么简单!但是,将 rip 保存到 rcx 的代码在哪里?那只是CPU电路吗?
    • @오준택 是的,在包含的链接上描述了什么,syscall 如何运行,是单个(从代码的角度来看)CPU 指令,在芯片的硬件中实现(可能有几个微说明,但这是实现细节)。
    • 哦,我明白了。谢谢!
    • @오준택:有一个 AMD64 邮件列表存档,其中包含内核开发人员和 AMD CPU 架构师在整理 syscall / swapgs / sysret 的设计时的一些消息。屏蔽RFLAGS(并将旧的RFLAGS 保存到r11)基本上是应Linux 内核开发人员的要求。有关链接,请参阅stackoverflow.com/questions/4429398/…。顺便说一句,保存到寄存器意味着syscall 本身不必修改rsp 或在切换到内核模式后将任何内容存储到内存中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多