What exactly happens in virtual memory when I call a function like printf in Linux? [on hold]
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls.
What is the step-by-step description in detail for what happens? Because I'm a little confused in this area, I have these questions:
Are the instructions for the printf function inside of the kernel part of our user process? And when it tries to execute printf, we do a JMP to that kernel location within the same user process, but we go into kernel mode? Or is there a context switch and a kernel process executes this?
Do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?
Overall, what are the situations that non-kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
put on hold as too broad by Goro, RalfFriedl, Isaac, Thomas, Shadur 16 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
3
down vote
favorite
I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls.
What is the step-by-step description in detail for what happens? Because I'm a little confused in this area, I have these questions:
Are the instructions for the printf function inside of the kernel part of our user process? And when it tries to execute printf, we do a JMP to that kernel location within the same user process, but we go into kernel mode? Or is there a context switch and a kernel process executes this?
Do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?
Overall, what are the situations that non-kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
put on hold as too broad by Goro, RalfFriedl, Isaac, Thomas, Shadur 16 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
2
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls.
What is the step-by-step description in detail for what happens? Because I'm a little confused in this area, I have these questions:
Are the instructions for the printf function inside of the kernel part of our user process? And when it tries to execute printf, we do a JMP to that kernel location within the same user process, but we go into kernel mode? Or is there a context switch and a kernel process executes this?
Do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?
Overall, what are the situations that non-kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls.
What is the step-by-step description in detail for what happens? Because I'm a little confused in this area, I have these questions:
Are the instructions for the printf function inside of the kernel part of our user process? And when it tries to execute printf, we do a JMP to that kernel location within the same user process, but we go into kernel mode? Or is there a context switch and a kernel process executes this?
Do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?
Overall, what are the situations that non-kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
kernel memory virtual-memory
edited yesterday
Peter Mortensen
82558
82558
asked yesterday
John P
1608
1608
put on hold as too broad by Goro, RalfFriedl, Isaac, Thomas, Shadur 16 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as too broad by Goro, RalfFriedl, Isaac, Thomas, Shadur 16 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
2
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
add a comment |Â
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
2
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
2
2
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
16
down vote
accepted
printf
is implemented by the C library, itâÂÂs not part of the kernel. (The kernel does have its own equivalent, more or less, but thatâÂÂs not available to user processes.) So a user process callingprintf
doesnâÂÂt call into the kernel immediately. Ifprintf
âÂÂs output gets writtenù, that happens by callingwrite
, which is handled by the kernel (well, thereâÂÂs a small wrapper in the C library, but itâÂÂs minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes,
printf
maps to the same physical address, if itâÂÂs provided by the same library.The kernel part of virtual memory is only accessible from kernel code.
ù Strictly speaking, printf
writes its output to a buffer, which might not be written anywhere.
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process callswrite
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).
â Stephen Kitt
yesterday
2
printf
may end-up callingwrite()
. It adds some text to a stdio buffer which may eventually be written.
â Stéphane Chazelas
yesterday
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,INT 0x80
,SYSENTER
, orSYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.
â Stephen Kitt
22 hours ago
 |Â
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
printf
is implemented by the C library, itâÂÂs not part of the kernel. (The kernel does have its own equivalent, more or less, but thatâÂÂs not available to user processes.) So a user process callingprintf
doesnâÂÂt call into the kernel immediately. Ifprintf
âÂÂs output gets writtenù, that happens by callingwrite
, which is handled by the kernel (well, thereâÂÂs a small wrapper in the C library, but itâÂÂs minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes,
printf
maps to the same physical address, if itâÂÂs provided by the same library.The kernel part of virtual memory is only accessible from kernel code.
ù Strictly speaking, printf
writes its output to a buffer, which might not be written anywhere.
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process callswrite
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).
â Stephen Kitt
yesterday
2
printf
may end-up callingwrite()
. It adds some text to a stdio buffer which may eventually be written.
â Stéphane Chazelas
yesterday
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,INT 0x80
,SYSENTER
, orSYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.
â Stephen Kitt
22 hours ago
 |Â
show 3 more comments
up vote
16
down vote
accepted
printf
is implemented by the C library, itâÂÂs not part of the kernel. (The kernel does have its own equivalent, more or less, but thatâÂÂs not available to user processes.) So a user process callingprintf
doesnâÂÂt call into the kernel immediately. Ifprintf
âÂÂs output gets writtenù, that happens by callingwrite
, which is handled by the kernel (well, thereâÂÂs a small wrapper in the C library, but itâÂÂs minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes,
printf
maps to the same physical address, if itâÂÂs provided by the same library.The kernel part of virtual memory is only accessible from kernel code.
ù Strictly speaking, printf
writes its output to a buffer, which might not be written anywhere.
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process callswrite
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).
â Stephen Kitt
yesterday
2
printf
may end-up callingwrite()
. It adds some text to a stdio buffer which may eventually be written.
â Stéphane Chazelas
yesterday
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,INT 0x80
,SYSENTER
, orSYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.
â Stephen Kitt
22 hours ago
 |Â
show 3 more comments
up vote
16
down vote
accepted
up vote
16
down vote
accepted
printf
is implemented by the C library, itâÂÂs not part of the kernel. (The kernel does have its own equivalent, more or less, but thatâÂÂs not available to user processes.) So a user process callingprintf
doesnâÂÂt call into the kernel immediately. Ifprintf
âÂÂs output gets writtenù, that happens by callingwrite
, which is handled by the kernel (well, thereâÂÂs a small wrapper in the C library, but itâÂÂs minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes,
printf
maps to the same physical address, if itâÂÂs provided by the same library.The kernel part of virtual memory is only accessible from kernel code.
ù Strictly speaking, printf
writes its output to a buffer, which might not be written anywhere.
printf
is implemented by the C library, itâÂÂs not part of the kernel. (The kernel does have its own equivalent, more or less, but thatâÂÂs not available to user processes.) So a user process callingprintf
doesnâÂÂt call into the kernel immediately. Ifprintf
âÂÂs output gets writtenù, that happens by callingwrite
, which is handled by the kernel (well, thereâÂÂs a small wrapper in the C library, but itâÂÂs minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes,
printf
maps to the same physical address, if itâÂÂs provided by the same library.The kernel part of virtual memory is only accessible from kernel code.
ù Strictly speaking, printf
writes its output to a buffer, which might not be written anywhere.
edited 22 hours ago
answered yesterday
Stephen Kitt
149k23332399
149k23332399
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process callswrite
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).
â Stephen Kitt
yesterday
2
printf
may end-up callingwrite()
. It adds some text to a stdio buffer which may eventually be written.
â Stéphane Chazelas
yesterday
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,INT 0x80
,SYSENTER
, orSYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.
â Stephen Kitt
22 hours ago
 |Â
show 3 more comments
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process callswrite
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).
â Stephen Kitt
yesterday
2
printf
may end-up callingwrite()
. It adds some text to a stdio buffer which may eventually be written.
â Stéphane Chazelas
yesterday
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,INT 0x80
,SYSENTER
, orSYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.
â Stephen Kitt
22 hours ago
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
â John P
yesterday
Generally, the process calls
write
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).â Stephen Kitt
yesterday
Generally, the process calls
write
, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (thatâÂÂs why itâÂÂs a system call).â Stephen Kitt
yesterday
2
2
printf
may end-up calling write()
. It adds some text to a stdio buffer which may eventually be written.â Stéphane Chazelas
yesterday
printf
may end-up calling write()
. It adds some text to a stdio buffer which may eventually be written.â Stéphane Chazelas
yesterday
1
1
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
@John I was referring to the part of memory reserved for the kernel.
â Stephen Kitt
22 hours ago
1
1
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,
INT 0x80
, SYSENTER
, or SYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.â Stephen Kitt
22 hours ago
@John a system call doesnâÂÂt involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You donâÂÂt jump to an address in the kernelâÂÂs end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86,
INT 0x80
, SYSENTER
, or SYSCALL
, depending on the specific ABI being used). See this article and its follow-up for details.â Stephen Kitt
22 hours ago
 |Â
show 3 more comments
read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday
2
@ç¥Âç§Âå¾·éÂÂå  what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
â John P
yesterday
Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer.
â ç¥Âç§Âå¾·éÂÂå Â
yesterday