AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
arch.h File Reference
#include <core/types.h>
#include <sys/proc.h>
#include <dev/irq.h>

Go to the source code of this file.

Functions

void arch_proc_init (struct proc *proc)
 Sets up the architecture's part of a new process's thread (init's, at boot): its kernel stack, and a start at the program's entry point in user mode.
void arch_sleep (void)
 Puts the current thread to sleep.
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 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.
void arch_idle (void)
 Makes this CPU idle, on its idle stack, until an interrupt makes a thread ready.
int arch_proc_fork (struct thread *thread, struct proc *fork)
 Sets up the CPU state of a fork() child's thread: a copy of the parent's kernel stack, registers and FPU state, so that the child returns from fork() as its parent does.
void arch_syscall_return (struct thread *thread, uintptr_t val)
 Sets the result of a thread's system call, in the register that returns it to user mode.
void arch_sched_init (void)
 Starts the timer tick on every CPU, and the clock where it needs to be started.
void arch_cur_thread_kill (void)
 Ends the current thread.
void arch_yield (void)
 Puts the current thread at the end of the ready list, and runs the next ready thread.
void arch_exec_start (struct thread *thread, uintptr_t entry, uintptr_t sp)
 Sets up a thread to start a new program when it next returns to user mode: at its entry point, with its stack pointer at its initial stack, which the kernel has built already (argc, argv, envp and the auxiliary vector, as the System V ABI has them: sys/execve.c).
void arch_sigreturn (void)
 Returns from a signal handler, for sigreturn(): restores the registers, the signal mask and the FPU state that the handler's frame saved.
int arch_sigcode_map (struct proc *proc)
 Maps the page with the code that calls sigreturn() (the sigcode) into a process's new address space.
void arch_mm_setup (void)
 Finishes setting up memory at boot, once mm_setup() has set up the page structures and the buddy allocator: the kernel's page tables.
paddr_t arch_page_get_mapping (struct pmap *pmap, vaddr_t vaddr)
 Finds the page mapped at a virtual address of an address space.
void * arch_phys_map (uintptr_t paddr, size_t size)
 Returns a kernel address for a range of physical memory: its address in the direct map if it's there, or a mapping made in a window.
void * arch_mmio_map (uintptr_t paddr, size_t size)
 Maps a device's registers into the kernel's address space, uncached (memory-mapped I/O).
int arch_msi_alloc (irq_handler_t handler, void *arg, uint64_t *addr, uint32_t *data)
 Gets an IRQ for a device's MSI, and installs its handler.
void arch_reboot (void)
 Restarts the machine.
void arch_halt (void)
 Stops the CPU for good, with interrupts off.
void arch_poweroff (void)
 Powers the machine off, or halts it if it can't be powered off.
uint64_t arch_rtime_ns (void)
 Reads the monotonic clock, which starts at boot.
uint64_t arch_rtime_us (void)
 Reads the monotonic clock, in microseconds.
uint64_t arch_rtime_ms (void)
 Reads the monotonic clock, in milliseconds.
uint64_t arch_cycles (void)
 Reads a counter that changes as fast as the CPU runs, which the kernel uses as a source of randomness.
int arch_time_get (struct timespec *ts)
 Reads the date and time from the real time clock.
void arch_stack_trace (void)
 Prints the stack trace of the running code, for a panic: the return address of each frame.

Function Documentation

◆ arch_cur_thread_kill()

void arch_cur_thread_kill ( void )

Ends the current thread.

Its kernel stack is freed with it, so this switches to the CPU's idle stack first. It doesn't return.

Definition at line 87 of file sched.c.

References __arch_cur_thread_kill(), aarch64_goto(), arch_cur_thread_kill(), and x86_goto().

Referenced by arch_cur_thread_kill(), proc_kill(), sched_thread_killed(), and sys_thread_exit().

◆ arch_cycles()

uint64_t arch_cycles ( void )

Reads a counter that changes as fast as the CPU runs, which the kernel uses as a source of randomness.

Returns
the counter's value

Definition at line 82 of file timer.c.

References arch_cycles(), and read_sysreg.

Referenced by arch_cycles(), random_stir(), and sched_timer_tick().

◆ arch_exec_start()

void arch_exec_start ( struct thread * thread,
uintptr_t entry,
uintptr_t sp )

Sets up a thread to start a new program when it next returns to user mode: at its entry point, with its stack pointer at its initial stack, which the kernel has built already (argc, argv, envp and the auxiliary vector, as the System V ABI has them: sys/execve.c).

The other registers start as zeros, and so does the FPU's state.

Parameters
threadthe thread, which hasn't run since execve() (or ever)
entrythe program's entry point
spthe initial stack pointer, 16-byte aligned

Definition at line 15 of file execve.c.

References aarch64_fpsimd_drop(), thread::arch, arch_exec_start(), aarch64_thread::pc, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, aarch64_thread::sp, and X86_EFLAGS.

Referenced by arch_exec_start(), and exec_start().

◆ arch_halt()

void arch_halt ( void )

Stops the CPU for good, with interrupts off.

Definition at line 38 of file psci.c.

References arch_halt(), and arch_interrupts_disable().

Referenced by arch_halt(), arch_poweroff(), and kernel_reboot().

◆ arch_idle()

void arch_idle ( void )

Makes this CPU idle, on its idle stack, until an interrupt makes a thread ready.

It doesn't return.

Definition at line 62 of file sched.c.

References __arch_idle(), aarch64_goto(), arch_idle(), curthread, NULL, VMA, x86_goto(), and x86_kernel_stack_set().

Referenced by arch_idle(), kernel_idle(), and x86_ap_start().

◆ 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, KERN_STACK_SIZE, kfree(), kmalloc(), aarch64_thread::kstack, x86_thread::kstack, x86_thread::kstack_base, aarch64_ctx::lr, M_ZERO, NULL, PUSH, x86_thread::rbp, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, aarch64_ctx::sp, and X86_EFLAGS.

Referenced by arch_kthread_create(), and kthread_create().

◆ arch_mm_setup()

void arch_mm_setup ( void )

Finishes setting up memory at boot, once mm_setup() has set up the page structures and the buddy allocator: the kernel's page tables.

Definition at line 514 of file pmap.c.

References arch_mm_setup(), buddy_set_unusable(), pmap_init(), X86_TRAMPOLINE, and X86_TRAMPOLINE_SIZE.

Referenced by arch_mm_setup(), and mm_setup().

◆ arch_mmio_map()

void * arch_mmio_map ( uintptr_t paddr,
size_t size )

Maps a device's registers into the kernel's address space, uncached (memory-mapped I/O).

Parameters
paddrthe registers' physical address
sizetheir size, in bytes
Returns
their kernel address, or NULL if they couldn't be mapped

Definition at line 508 of file pmap.c.

References arch_mmio_map(), phys_window_map(), VM_KRW, VM_NOCACHE, and window_map().

Referenced by arch_mmio_map(), e1000_probe(), pci_host_set(), platform_device_io(), x86_apic_setup(), and x86_hpet_init().

◆ arch_msi_alloc()

int arch_msi_alloc ( irq_handler_t handler,
void * arg,
uint64_t * addr,
uint32_t * data )

Gets an IRQ for a device's MSI, and installs its handler.

The message goes to the boot CPU's local APIC, with the IRQ's vector, edge-triggered.

Parameters
handlerthe interrupt's handler
argthe argument the handler gets
addrset to the address the device writes the message to
dataset to the message
Returns
the IRQ, -ENODEV without the APIC (the 8259s take no MSI), or -ENOSPC if there's no free IRQ

Definition at line 271 of file apic.c.

References arch_msi_alloc(), irq_install(), lapic, lapic_id, msi_chip, and x86_irq_alloc().

Referenced by arch_msi_alloc(), and pci_msi_enable().

◆ arch_page_get_mapping()

paddr_t arch_page_get_mapping ( struct pmap * pmap,
vaddr_t vaddr )

Finds the page mapped at a virtual address of an address space.

Parameters
pmapthe address space's page tables
vaddrthe virtual address
Returns
the page's physical address, or 0 if none is mapped there

Definition at line 431 of file pmap.c.

References arch_page_get_mapping(), D_ADDR, D_VALID, PG_ADDR, PG_PRESENT, and pte_get().

Referenced by arch_page_get_mapping(), kvmem_selftest_run(), kvmem_unmap(), mm_map(), mm_page_unmap(), and slab_page_free().

◆ arch_phys_map()

void * arch_phys_map ( uintptr_t paddr,
size_t size )

Returns a kernel address for a range of physical memory: its address in the direct map if it's there, or a mapping made in a window.

Parameters
paddrthe range's physical address
sizeits size, in bytes
Returns
its kernel address, or NULL if it couldn't be mapped

Definition at line 497 of file pmap.c.

References arch_phys_map(), dmap_end, PHYS2VIRT, phys_window_map(), VM_KRW, and window_map().

Referenced by arch_phys_map(), netbuf_alloc(), page_get(), and rd_probe().

◆ arch_poweroff()

void arch_poweroff ( void )

Powers the machine off, or halts it if it can't be powered off.

Definition at line 31 of file psci.c.

References arch_halt(), arch_poweroff(), psci(), PSCI_SYSTEM_OFF, and x86_acpi_poweroff().

Referenced by arch_poweroff(), and kernel_reboot().

◆ arch_proc_fork()

int arch_proc_fork ( struct thread * thread,
struct proc * fork )

Sets up the CPU state of a fork() child's thread: a copy of the parent's kernel stack, registers and FPU state, so that the child returns from fork() as its parent does.

Parameters
threadthe thread that called fork()
forkthe child process, whose one thread gets the state
Returns
0, or -ENOMEM if there was no memory for it

Definition at line 21 of file fork.c.

References aarch64_fpsimd_fork(), aarch64_thread_new(), thread::arch, arch_proc_fork(), aarch64_thread::ctx, x86_regs::err, aarch64_ctx::fp, x86_thread::fpu_context, x86_thread::fpu_enabled, queue::head, KERN_STACK_SIZE, kfree(), kmalloc(), aarch64_thread::kstack, x86_thread::kstack, aarch64_thread::kstack_base, x86_thread::kstack_base, aarch64_ctx::lr, M_ZERO, memcpy(), NULL, aarch64_thread::regs, x86_thread::regs, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, aarch64_ctx::sp, proc::threads, qnode::value, and x86_fpu_save_to().

Referenced by arch_proc_fork(), and proc_fork().

◆ arch_proc_init()

void arch_proc_init ( struct proc * proc)

Sets up the architecture's part of a new process's thread (init's, at boot): its kernel stack, and a start at the program's entry point in user mode.

Parameters
procthe process

Definition at line 19 of file proc.c.

References aarch64_thread_new(), thread::arch, arch_proc_init(), proc::entry, queue::head, KERN_STACK_SIZE, kmalloc(), x86_thread::kstack, x86_thread::kstack_base, M_ZERO, panic, aarch64_thread::pc, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, aarch64_thread::sp, proc::threads, USER_STACK, qnode::value, and X86_EFLAGS.

Referenced by arch_proc_init(), and kmain().

◆ arch_reboot()

void arch_reboot ( void )

Restarts the machine.

Definition at line 25 of file psci.c.

References arch_reboot(), platform_reboot(), psci(), and PSCI_SYSTEM_RESET.

Referenced by arch_reboot(), kernel_ctrlaltdel(), kernel_reboot(), and proc_kill().

◆ arch_rtime_ms()

uint64_t arch_rtime_ms ( void )

Reads the monotonic clock, in milliseconds.

Returns
the time since the clock started, in milliseconds

Definition at line 73 of file timer.c.

References arch_rtime_ms(), and arch_rtime_ns().

Referenced by arch_rtime_ms(), arp_lookup(), arp_update(), and procfs_uptime().

◆ arch_rtime_ns()

◆ arch_rtime_us()

uint64_t arch_rtime_us ( void )

Reads the monotonic clock, in microseconds.

Returns
the time since the clock started, in microseconds

Definition at line 65 of file timer.c.

References arch_rtime_ns(), and arch_rtime_us().

Referenced by arch_rtime_us().

◆ arch_sched_init()

void arch_sched_init ( void )

Starts the timer tick on every CPU, and the clock where it needs to be started.

Definition at line 43 of file sched.c.

References arch_cpu_kick(), arch_sched_init(), cpus, cpus_count, platform_timer_setup(), sched_tick(), timer_period, timer_setup(), x86_clock_init(), x86_clock_ticks(), and x86_sched_handler().

Referenced by arch_sched_init(), and sched_init_spawn().

◆ arch_sigcode_map()

int arch_sigcode_map ( struct proc * proc)

Maps the page with the code that calls sigreturn() (the sigcode) into a process's new address space.

Parameters
procthe process
Returns
0, or a negative errno (-ENOMEM if there was no memory)

Definition at line 107 of file signal.c.

References arch_sigcode_map(), vm_entry::base, vm_entry::flags, kfree(), kmalloc(), M_ZERO, PAGE_SIZE, vm_object::pages, SIGCODE_ADDR, sigcode_object, vm_entry::size, vm_entry::vm_object, vm_object_incref(), vm_page_eq(), proc::vm_space, vm_space_insert(), and VM_URX.

Referenced by arch_sigcode_map(), and binfmt_fmt_load().

◆ arch_sigreturn()

void arch_sigreturn ( void )

Returns from a signal handler, for sigreturn(): restores the registers, the signal mask and the FPU state that the handler's frame saved.

A bad frame gets SIGSEGV.

Definition at line 247 of file signal.c.

References aarch64_fpsimd_restore(), arch_sigreturn(), x86_regs::cs, curproc, curthread, sigframe::fpsimd, sigframe::fpsimd_valid, sigframe::fpu, sigframe::fxsave, I386_ARGS, sigframe::mask, NULL, aarch64_regs::pstate, REG_FLAGS, REG_SP, aarch64_thread::regs, sigframe::regs, x86_thread::regs, SIG_UNMASKABLE, signal_force(), aarch64_regs::sp, x86_regs::ss, USER_FLAGS, X86_CS, X86_EFLAGS, x86_fpu_load_from(), and X86_SS.

Referenced by arch_sigreturn(), and sys_sigreturn().

◆ arch_sleep()

void arch_sleep ( void )

Puts the current thread to sleep.

It stops here, and the next ready thread runs, or the CPU idles; it returns once the thread is woken up and chosen to run again.

Definition at line 101 of file sched.c.

References aarch64_ctx_save(), arch_sleep(), aarch64_thread::ctx, curthread, and kernel_idle().

Referenced by arch_sleep(), proc_execve(), signal_exit(), sleep_on(), and sys_exit().

◆ arch_stack_trace()

void arch_stack_trace ( void )

Prints the stack trace of the running code, for a panic: the return address of each frame.

Definition at line 14 of file trace.c.

References __kboot, __printing_trace, ARCH_DMAP_BASE, arch_stack_trace(), stack_frame::bp, stack_frame::ip, NULL, printk(), elf32_shdr::sh_addr, elf32_sym::st_name, elf32_sym::st_value, boot::strtab, and symbol_of().

Referenced by arch_stack_trace().

◆ arch_syscall_return()

void arch_syscall_return ( struct thread * thread,
uintptr_t val )

Sets the result of a thread's system call, in the register that returns it to user mode.

That's in the thread's saved registers, or, for a thread that has never run (a child of fork()), where it will start from.

Parameters
threadthe thread
valthe result

Definition at line 43 of file syscall.c.

References thread::arch, arch_syscall_return(), x86_regs::rax, x86_thread::rax, aarch64_thread::regs, x86_thread::regs, thread::spawned, aarch64_regs::x, and aarch64_thread::x0.

Referenced by arch_syscall_return(), proc_fork(), and syscall_dispatch().

◆ 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(), KERN_STACK_SIZE, kfree(), kmalloc(), x86_thread::kstack, x86_thread::kstack_base, M_ZERO, NULL, aarch64_thread::pc, PUSH, x86_thread::rflags, x86_thread::rip, x86_thread::rsp, aarch64_thread::sp, aarch64_thread::x0, aarch64_thread::x1, and X86_EFLAGS.

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(), cpu::arch, thread::arch, arch_thread_kill(), cpus, cpus_count, curthread, x86_thread::fpu_context, arch_cpu::fpu_thread, kfree(), aarch64_thread::kstack_base, x86_thread::kstack_base, NULL, VMA, and x86_kernel_stack_set().

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, 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().

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, 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().

Referenced by arch_thread_switch(), and schedule().

◆ arch_time_get()

int arch_time_get ( struct timespec * ts)

Reads the date and time from the real time clock.

Parameters
tsset to the time since the epoch, to the second (tv_nsec is 0)
Returns
0

Definition at line 16 of file rtc.c.

References arch_time_get(), cmos_time(), cmos_rtc::hrs, cmos_rtc::mday, cmos_rtc::min, cmos_rtc::mon, RTC_DR, cmos_rtc::sec, timespec::tv_nsec, timespec::tv_sec, VIRT_REG, VIRT_RTC, and cmos_rtc::yr.

Referenced by arch_time_get(), gettime(), and time_resync().

◆ arch_yield()

void arch_yield ( void )

Puts the current thread at the end of the ready list, and runs the next ready thread.

Definition at line 94 of file sched.c.

References aarch64_preempt(), and arch_yield().

Referenced by arch_yield(), and net_main().