AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
thread.c File Reference

x86: a thread's CPU state, its kernel stack, and switching to it. More...

#include <core/system.h>
#include <core/string.h>
#include <core/arch.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/signal.h>
#include <sys/lock.h>
#include <sys/uaccess.h>
#include <aquila/syscall.h>
#include <ds/queue.h>
#include <mm/pmap.h>

Go to the source code of this file.

Macros

#define PUSH(stack, type, value)

Functions

static void thread_spawn (void)
 Starts the current thread in user mode, for the first time, from its own kernel stack.
void arch_thread_spawn (struct thread *thread)
 Runs a thread for the first time: switches to its address space and kernel stack, and starts it.
void arch_thread_switch (struct thread *thread)
 Switches to a thread that has run before: its address space, its kernel stack, then where it stopped.
int arch_thread_create (struct thread *thread, uintptr_t stack, uintptr_t entry, uintptr_t uentry, uintptr_t arg)
 Sets up a new user thread's CPU state and kernel stack, for pthread_create().
void arch_kthread_create (struct thread *thread, uintptr_t entry)
 Sets up a kernel thread's CPU state.
void arch_thread_kill (struct thread *thread)
 Frees a thread's CPU state and kernel stack.
void x86_thread_saved (uintptr_t ip, uintptr_t sp, uintptr_t bp)
 Records where the current thread resumes when it is switched back to (x86_goto()), with its registers saved on its kernel stack.

Detailed Description

x86: a thread's CPU state, its kernel stack, and switching to it.

Definition in file thread.c.

Macro Definition Documentation

◆ PUSH

#define PUSH ( stack,
type,
value )
Value:
do {\
(stack) -= sizeof(type);\
*((type *) (stack)) = (type) (value);\
} while (0)

Definition at line 21 of file thread.c.

Function Documentation

◆ arch_kthread_create()

void arch_kthread_create ( struct thread * thread,
uintptr_t entry )

Sets up a kernel thread's CPU state.

The thread starts at entry, a function without arguments that never returns, on its own kernel stack, once it is switched to.

Parameters
threadthe new thread; its arch stays NULL if there was no memory for its state
entrythe function it runs

Definition at line 146 of file thread.c.

References thread::arch, arch_kthread_create(), KERN_STACK_SIZE, kfree(), kmalloc(), x86_thread::kstack, x86_thread::kstack_base, M_ZERO, NULL, PUSH, x86_thread::rbp, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, and X86_EFLAGS.

◆ arch_thread_create()

int arch_thread_create ( struct thread * thread,
uintptr_t stack,
uintptr_t entry,
uintptr_t uentry,
uintptr_t arg )

Sets up a new user thread's CPU state and kernel stack, for pthread_create().

The thread will start at entry in user mode, on its user stack, and that code calls uentry with arg.

Parameters
threadthe new thread
stackthe top of its user stack
entrywhere it starts in user mode (the C library's thread start routine)
uentrythe function that routine calls (the user's function)
argthat function's argument
Returns
0, -ENOMEM if there was no memory for the thread's state, or -EFAULT if the stack isn't the process's memory

Definition at line 91 of file thread.c.

References thread::arch, arch_thread_create(), KERN_STACK_SIZE, kfree(), kmalloc(), x86_thread::kstack, x86_thread::kstack_base, M_ZERO, NULL, PUSH, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, and X86_EFLAGS.

◆ arch_thread_kill()

void arch_thread_kill ( struct thread * thread)

Frees a thread's CPU state and kernel stack.

The thread must not be running on them.

Parameters
threadthe thread

Definition at line 186 of file thread.c.

References cpu::arch, thread::arch, arch_thread_kill(), cpus, cpus_count, curthread, x86_thread::fpu_context, arch_cpu::fpu_thread, kfree(), x86_thread::kstack_base, NULL, VMA, and x86_kernel_stack_set().

◆ arch_thread_spawn()

void arch_thread_spawn ( struct thread * thread)

Runs a thread for the first time: switches to its address space and kernel stack, and starts it.

It doesn't return.

Parameters
threadthe thread, which has never run

Definition at line 47 of file thread.c.

References thread::arch, arch_thread_spawn(), x86_thread::kstack, thread::owner, vm_space::pmap, pmap_switch(), thread_spawn(), proc::vm_space, x86_fpu_disable(), x86_goto(), and x86_kernel_stack_set().

◆ arch_thread_switch()

void arch_thread_switch ( struct thread * thread)

Switches to a thread that has run before: its address space, its kernel stack, then where it stopped.

It doesn't return.

Parameters
threadthe thread

Definition at line 67 of file thread.c.

References thread::arch, arch_thread_switch(), x86_thread::kstack, thread::owner, vm_space::pmap, pmap_switch(), x86_thread::rbp, x86_thread::rip, x86_thread::rsp, proc::vm_space, x86_fpu_disable(), x86_goto(), and x86_kernel_stack_set().

◆ thread_spawn()

void thread_spawn ( void )
static

Starts the current thread in user mode, for the first time, from its own kernel stack.

It lets go of the kernel lock first; once it does, another CPU may run the thread that was running here.

Definition at line 30 of file thread.c.

References curthread, kernel_unlock(), x86_thread::rax, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, thread_spawn(), X86_CS, x86_jump_user(), and X86_SS.

◆ x86_thread_saved()

void x86_thread_saved ( uintptr_t ip,
uintptr_t sp,
uintptr_t bp )

Records where the current thread resumes when it is switched back to (x86_goto()), with its registers saved on its kernel stack.

x86_sleep and x86_preempt call it, and it saves the thread's FPU state too.

Parameters
ipwhere it resumes
spits stack pointer there
bpits frame pointer there

Definition at line 227 of file thread.c.

References curthread, x86_thread::rbp, x86_thread::rip, x86_thread::rsp, x86_fpu_switch_out(), and x86_thread_saved().

Referenced by x86_thread_saved().