Reputation: 401
I know that kernel mode stack is stored with thread_info structure of task_struct structure. But where is user mode stack stored. I guess, it will be stored in process address space as a memory region, because during page fault kernel checks if the fault was due to user stack expansion. I need more details about user stack. And what is the purpose of ss(stack segment register)
Upvotes: 1
Views: 514
Reputation: 1244
It's stored on the kernel stack.
The Linux syscall entry code is pretty hairy, especially now that it's performing some spectre and meltdown mitigations, but you can look at the definition of entry_SYSCALL_64
.
Specifically, this sequence saves the userland thread's state to the kernel stack. It's constructing the last part of the struct pt_regs
structure which it will later pass to do_syscall_64
.
/* Construct struct pt_regs on stack */
pushq $__USER_DS /* pt_regs->ss */
pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */ // This is where it's put on the stack.
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
As for the ss register, before x86 had virtual memory there was this idea of segments. Each program would live in its own series of memory segments. Each segment register (ss, gs, etc.) held an index into the Global Descriptor Table which determined where the segment started and what permissions it had. The ss held the segment for the stack. If you tried to push, pop, or call if esp pointed outside the stack segment you would get a segmentation exception. These days with x86_64 ss and most of the other segmentation registers are mostly vestigial, except for fs and gs which are used to access thread local data in user space and kernel space respectively.
Upvotes: 1