問題描述
將參數傳遞給系統調用 (Passing arguments to system calls)
我知道當我們想要進行系統調用時,我們通常會圍繞該系統調用調用一個 C 包裝器,該包裝器在內部將參數放在正確的位置(有時在特定的寄存器或堆棧中)。
之後放置參數(並忽略諸如 syscall 指令之類的方法,因為它並非在所有平台上都可用),包裝器通常執行 int 0x80(軟件中斷)。
現在,當遇到中斷時,硬件會將正在運行的進程的所有寄存器保存在其堆棧中並進行上下文切換(這將包括切換程序計數器和堆棧指針等)。
我無法理解系統調用處理程序如何訪問最初由 C 包裝器在特定寄存器中設置的參數,這些參數現在駐留在 user 進程堆棧中(not in 內核進程堆棧)
參考解法
方法 1:
Now, when an interrupt is encountered hardware will save all the registers of the running process on its stack and make context switch (which will include switching program counter and stack pointer among other things).
Nope ;‑) On Linux AMD64, syscall arguments are directly passed in registers (registers do not get stacked).
For details, see "x86‑64 Linux System Call convention" http://refspecs.linuxfoundation.org/elf/x86_64‑abi‑0.99.pdf , Sect. 'A.2.1 Calling Conventions' at the bottom of page 123/head of p. 124.
(by Rahul Patel、senatores conscripti)