The Pedigree Project 0.1
arm64/state.cc
1#include "pedigree/kernel/processor/state.h"
2
3processor_register_t Arm64InterruptState::getRegister(size_t index) const {
4 if (index < 31) {
5 return x[index];
6 }
7 if (index == 31) {
8 return sp;
9 }
10 if (index == 32) {
11 return pc;
12 }
13 return index == 33 ? pstate : 0;
14}
15
16void Arm64InterruptState::setRegister(size_t index, uintptr_t value) {
17 if (index < 31) {
18 x[index] = value;
19 } else if (index == 31) {
20 sp = value;
21 } else if (index == 32) {
22 pc = value;
23 } else if (index == 33) {
24 pstate = value;
25 }
26}
27
28const char* Arm64InterruptState::getRegisterName(size_t index) const {
29 static const char* const names[] = {
30 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",
31 "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
32 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp", "pc", "pstate"};
33 return index < 34 ? names[index] : "?";
34}
35
36Arm64InterruptState* Arm64InterruptState::construct(Arm64ProcessorState& state, bool userMode) {
37 uintptr_t frameAddress = (state.sp - sizeof(Arm64InterruptState)) & ~uintptr_t(15);
38 auto* frame = reinterpret_cast<Arm64InterruptState*>(frameAddress);
39 for (size_t i = 0; i < 31; ++i) {
40 frame->x[i] = state.x[i];
41 }
42 frame->sp = state.sp;
43 frame->pc = state.pc;
44 frame->pstate = userMode ? 0 : 5;
45 frame->esr = 0;
46 frame->far = 0;
47 frame->vector = 0;
48 return frame;
49}