summaryrefslogtreecommitdiff
path: root/Kernel/Arch/aarch64/ThreadRegisters.h
blob: 30843c5324bfd38a0cd931ce5e9d3687ed0a7c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Types.h>
#include <Kernel/Library/StdLib.h>
#include <Kernel/Memory/AddressSpace.h>

namespace Kernel {

struct ThreadRegisters {
    u64 x[31];
    u64 spsr_el1;
    u64 elr_el1;
    u64 sp_el0;
    u64 ttbr0_el1;

    FlatPtr ip() const { return elr_el1; }
    void set_ip(FlatPtr value) { elr_el1 = value; }

    FlatPtr sp() const { return sp_el0; }
    void set_sp(FlatPtr value) { sp_el0 = value; }

    void set_initial_state(bool is_kernel_process, Memory::AddressSpace& space, FlatPtr kernel_stack_top)
    {
        set_sp(kernel_stack_top);
        ttbr0_el1 = space.page_directory().ttbr0();
        set_spsr_el1(is_kernel_process);
    }

    void set_entry_function(FlatPtr entry_ip, FlatPtr entry_data)
    {
        set_ip(entry_ip);
        x[0] = entry_data;
    }

    void set_exec_state(FlatPtr entry_ip, FlatPtr userspace_sp, Memory::AddressSpace& space)
    {
        set_ip(entry_ip);
        set_sp(userspace_sp);
        ttbr0_el1 = space.page_directory().ttbr0();
        set_spsr_el1(false);
    }

    void set_spsr_el1(bool is_kernel_process)
    {
        Aarch64::SPSR_EL1 saved_program_status_register_el1 = {};

        // Don't mask any interrupts, so all interrupts are enabled when transfering into the new context
        saved_program_status_register_el1.D = 0;
        saved_program_status_register_el1.A = 0;
        saved_program_status_register_el1.I = 0;
        saved_program_status_register_el1.F = 0;

        saved_program_status_register_el1.M = is_kernel_process ? Aarch64::SPSR_EL1::Mode::EL1h : Aarch64::SPSR_EL1::Mode::EL0t;
        memcpy(&spsr_el1, &saved_program_status_register_el1, sizeof(u64));
    }
};

}