xv6中open(const char*, const int)函数定义位置咨询
open() Implementation in xv6 Hey there! I totally get why you're confused—xv6's user-space system call wrappers aren't written in plain C, which is why your grep searches for open(const char*, const int) came up empty. Let's break this down:
The user-space
open()is an assembly wrapper, generated from a macro inuser/usys.S. This file uses aSYSCALLmacro to create tiny assembly stubs for every system call, includingopen. Look for the lineSYSCALL(open)in this file—when the compiler processes it, it expands into assembly code that:- Loads the system call number for
openinto the appropriate register (like%eaxon x86) - Puts your function arguments (
const char*path andintflags) into the correct registers per xv6's calling convention - Triggers a trap to enter the kernel (via
int 0x80on x86) - Handles the return value from the kernel's
sys_openand passes it back to the calling user program
- Loads the system call number for
Where's the function prototype? Check
user/user.h—you'll find the C declaration forint open(const char*, int);there. This is what user-space programs include to callopen()correctly.Why didn't your grep find anything? Because there's no hand-written C function for
openin the user-space code. The assembly stub is generated entirely by that macro, so searching for C-style function definitions won't turn it up.
To confirm, you can run this command in your xv6-public directory:
grep -n "SYSCALL(open)" user/usys.S
It should point you right to the line that generates the open wrapper.
内容的提问来源于stack exchange,提问作者Flacarile

