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

aarch64: making threads, starting them, switching to them, and ending them. More...

#include <core/system.h>
#include <core/arch.h>
#include <core/string.h>
#include <mm/pmap.h>
#include <mm/vm.h>
#include <sys/lock.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/uaccess.h>

Go to the source code of this file.

Functions

struct aarch64_thread * aarch64_thread_new (void)
 Makes a thread's aarch64 state, with its kernel stack.
static void thread_spawn (void)
 Runs a thread for the first time: lets go of the kernel lock, and drops to user mode.
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.

Detailed Description

aarch64: making threads, starting them, switching to them, and ending them.

A thread stops in the kernel by saving where it is (aarch64_ctx_save(): the registers that a call keeps, and its stack pointer) and calling the scheduler, on its own stack (sys/sched.c). It goes on when that context is loaded again (aarch64_ctx_load()). A thread that never ran starts on its kernel stack, and drops to user mode from there (thread_spawn()).

Definition in file thread.c.

Function Documentation

◆ aarch64_thread_new()

struct aarch64_thread * aarch64_thread_new ( void )

Makes a thread's aarch64 state, with its kernel stack.

Returns
the state, or NULL if there was no memory for it

Definition at line 30 of file thread.c.

References aarch64_thread_new(), KERN_STACK_SIZE, kfree(), kmalloc(), aarch64_thread::kstack, aarch64_thread::kstack_base, M_ZERO, and NULL.

Referenced by aarch64_thread_new(), arch_kthread_create(), arch_proc_fork(), arch_proc_init(), and arch_thread_create().

◆ 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 113 of file thread.c.

References aarch64_thread_new(), thread::arch, arch_kthread_create(), aarch64_thread::ctx, aarch64_ctx::fp, aarch64_thread::kstack, aarch64_ctx::lr, and aarch64_ctx::sp.

Referenced by arch_kthread_create(), and kthread_create().

◆ 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 92 of file thread.c.

References aarch64_thread_new(), thread::arch, arch_thread_create(), aarch64_thread::pc, aarch64_thread::sp, aarch64_thread::x0, and aarch64_thread::x1.

Referenced by arch_thread_create(), and thread_create().

◆ 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 133 of file thread.c.

References aarch64_fpsimd_drop(), thread::arch, arch_thread_kill(), kfree(), aarch64_thread::kstack_base, and NULL.

Referenced by arch_thread_kill(), and thread_kill().

◆ 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 63 of file thread.c.

References aarch64_fpsimd_switch(), aarch64_goto(), thread::arch, arch_thread_spawn(), aarch64_thread::kstack, thread::owner, vm_space::pmap, pmap_switch(), thread_spawn(), and proc::vm_space.

Referenced by arch_thread_spawn(), and sched_thread_spawn().

◆ 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 76 of file thread.c.

References aarch64_ctx_load(), aarch64_fpsimd_switch(), thread::arch, arch_thread_switch(), aarch64_thread::ctx, thread::owner, vm_space::pmap, pmap_switch(), and proc::vm_space.

Referenced by arch_thread_switch(), and schedule().

◆ thread_spawn()

void thread_spawn ( void )
static

Runs a thread for the first time: lets go of the kernel lock, and drops to user mode.

Definition at line 51 of file thread.c.

References aarch64_jump_user(), curthread, kernel_unlock(), aarch64_thread::kstack, aarch64_thread::pc, aarch64_thread::sp, thread_spawn(), aarch64_thread::x0, aarch64_thread::x1, and aarch64_thread::x2.

Referenced by arch_thread_spawn(), and thread_spawn().