X86-64 Linux系统为什么调用RCX,该值是什么意思?
我正在尝试使用sys_brk
syscall在Linux中分配一些内存。这是我尝试的内容:
BYTES_TO_ALLOCATE equ 0x08
section .text
global _start
_start:
mov rax, 12
mov rdi, BYTES_TO_ALLOCATE
syscall
mov rax, 60
syscall
事实是根据Linux调用惯例,我期望返回值在rax
register中(指向分配的内存指针)。 以下寄存器内容,
Syscall之前的
rax 0xc 12
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x0 0
rdi 0x8 8
我在GDB中运行了此操作,在制作sys_brk
syscall之后,我注意到Syscall之后的
rax 0x401000 4198400
rbx 0x0 0
rcx 0x40008c 4194444 ; <---- What does this value mean?
rdx 0x0 0
rsi 0x0 0
rdi 0x8 8
我不太了解RCX
在这种情况下登记的值。我分配了sys_brk
的8个字节开始的指针?
I'm trying to allocate some memory in linux with sys_brk
syscall. Here is what I tried:
BYTES_TO_ALLOCATE equ 0x08
section .text
global _start
_start:
mov rax, 12
mov rdi, BYTES_TO_ALLOCATE
syscall
mov rax, 60
syscall
The thing is as per linux calling convention I expected the return value to be in rax
register (pointer to the allocated memory). I ran this in gdb and after making sys_brk
syscall I noticed the following register contents
Before syscall
rax 0xc 12
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x0 0
rdi 0x8 8
After syscall
rax 0x401000 4198400
rbx 0x0 0
rcx 0x40008c 4194444 ; <---- What does this value mean?
rdx 0x0 0
rsi 0x0 0
rdi 0x8 8
I do not quite understand the value in the rcx
register in this case. Which one to use as a pointer to the beginning of 8 bytes I allocated with sys_brk
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与往常一样,系统调用返回值在
rax
中。参见是unix&amp; Linux系统致电I386和X86-64 。请注意,
sys_brk
与brk
/sbrk
posix functions的接口略有不同;请参阅 c库/内核差异brk(2 )
人页。具体而言, linuxsys_brk
sets 程序折断; ARG和返回值都是指示。参见 issembly x86 brk()brk()该答案需要投票,因为这是该问题上唯一的好问题。您问题的另一个有趣的部分是:
在这种情况下, “>
syscall
/ 指令旨在允许内核恢复用户空间执行,但仍然很快。syscall
不做任何负载或商店,它仅修改寄存器。它没有使用特殊寄存器保存返回地址,而只是使用常规整数寄存器。rcx = rip
andrflags
在内核返回到您的用户空间代码之后,这不是一个巧合。此不是的唯一方法是,如果ptrace
系统调用修改了该过程已保存的rcx
或r11
值在内核内。 (ptrace
是系统调用GDB使用)。在这种情况下,Linux将使用IRET
而不是sysret
返回用户空间,因为较慢的通用案例iret
可以做到这一点。 (请参阅如果您在64位代码中使用32位INT 0x80 Linux ABI会发生什么?对于Linux的System-call输入点的某些演练。主要是从32位到32位的入口点 在64位过程中syscall
。流程,不是 代码> syscall :
设置rcx = rip,r11 = rflags(因此,在执行
syscall
之前,内核甚至不可能看到这些regs的原始值)。masks
rflags
带有从配置寄存器(ia32_fmask
msr)的预配置掩码。这使内核可以禁用中断(如果)直到完成swapgs
和设置rsp
指向内核堆栈。即使cli
是入口点处的第一个指令,也会有一个脆弱性窗口。您还可以免费获得cld
通过掩盖df
SOREP MOVS
/STOS
即使用户空间也向上移动已经使用了std
。有趣的事实:AMD的第一个提议
syscall
/swapgs
设计没有掩盖rflags,而是他们在AMD64邮件列表上的内核开发人员的反馈(在2000年,第一个硅之前的几年)。跳到配置的
syscall
输入点(设置CS:RIP =ia32_lstar
)。我认为旧的cs
值在任何地方都没有保存。它无能为它的价值来自用户空间。
因此,
syscall
的设计需要一个clobbers注册的系统通话ABI,这就是为什么值是它们的原因。The system call return value is in
rax
, as always. See What are the calling conventions for UNIX & Linux system calls on i386 and x86-64.Note that
sys_brk
has a slightly different interface than thebrk
/sbrk
POSIX functions; see the C library/kernel differences section of the Linuxbrk(2)
man page. Specifically, Linuxsys_brk
sets the program break; the arg and return value are both pointers. See Assembly x86 brk() call use. That answer needs upvotes because it's the only good one on that question.The other interesting part of your question is:
You're seeing the mechanics of how the
syscall
/sysret
instructions are designed to allow the kernel to resume user-space execution but still be fast.syscall
doesn't do any loads or stores, it only modifies registers. Instead of using special registers to save a return address, it simply uses regular integer registers.It's not a coincidence that
RCX=RIP
andR11=RFLAGS
after the kernel returns to your user-space code. The only way for this not to be the case is if aptrace
system call modified the process's savedrcx
orr11
value while it was inside the kernel. (ptrace
is the system call gdb uses). In that case, Linux would useiret
instead ofsysret
to return to user space, because the slower general-caseiret
can do that. (See What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code? for some walk-through of Linux's system-call entry points. Mostly the entry points from 32-bit processes, not fromsyscall
in a 64-bit process, though.)Instead of pushing a return address onto the kernel stack (like
int 0x80
does),syscall
:sets RCX=RIP, R11=RFLAGS (so it's impossible for the kernel to even see the original values of those regs before you executed
syscall
).masks
RFLAGS
with a pre-configured mask from a config register (theIA32_FMASK
MSR). This lets the kernel disable interrupts (IF) until it's doneswapgs
and settingrsp
to point to the kernel stack. Even withcli
as the first instruction at the entry point, there'd be a window of vulnerability. You also getcld
for free by masking offDF
sorep movs
/stos
go upward even if user-space had usedstd
.Fun fact: AMD's first proposed
syscall
/swapgs
design didn't mask RFLAGS, but they changed it after feedback from kernel developers on the amd64 mailing list (in ~2000, a couple years before the first silicon).jumps to the configured
syscall
entry point (setting CS:RIP =IA32_LSTAR
). The oldCS
value isn't saved anywhere, I think.It doesn't do anything else, the kernel has to use
swapgs
to get access to an info block where it saved the kernel stack pointer, becausersp
still has its value from user-space.So the design of
syscall
requires a system-call ABI that clobbers registers, and that's why the values are what they are.