The Pedigree Project 0.1
armv7/state.cc
1#include "pedigree/kernel/processor/state.h"
2
3processor_register_t Armv7InterruptState::getRegister(size_t index) const {
4 if (index < 13) {
5 return r[index];
6 }
7 if (index == 13) {
8 return sp;
9 }
10 if (index == 14) {
11 return lr;
12 }
13 if (index == 15) {
14 return pc;
15 }
16 if (index == 16) {
17 return cpsr;
18 }
19 if (index == 17) {
20 return faultAddress;
21 }
22 return index == 18 ? faultStatus : 0;
23}
24
25void Armv7InterruptState::setRegister(size_t index, uintptr_t value) {
26 if (index < 13) {
27 r[index] = value;
28 } else if (index == 13) {
29 sp = value;
30 } else if (index == 14) {
31 lr = value;
32 } else if (index == 15) {
33 pc = value;
34 } else if (index == 16) {
35 cpsr = value;
36 } else if (index == 17) {
37 faultAddress = value;
38 } else if (index == 18) {
39 faultStatus = value;
40 }
41}
42
43const char* Armv7InterruptState::getRegisterName(size_t index) const {
44 static const char* const names[] = {"r0", "r1", "r2", "r3", "r4", "r5", "r6",
45 "r7", "r8", "r9", "r10", "r11", "r12", "sp",
46 "lr", "pc", "cpsr", "far", "fsr"};
47 return index < 19 ? names[index] : "?";
48}
49
50Armv7InterruptState* Armv7InterruptState::construct(Armv7ProcessorState& state, bool userMode) {
51 uintptr_t frameAddress = (state.sp - sizeof(Armv7InterruptState)) & ~uintptr_t(7);
52 auto* frame = reinterpret_cast<Armv7InterruptState*>(frameAddress);
53 for (size_t i = 0; i < 13; ++i) {
54 frame->r[i] = state.r[i];
55 }
56 frame->sp = state.sp;
57 frame->lr = state.lr;
58 frame->pc = state.pc;
59 frame->cpsr = userMode ? (0x10 | (state.cpsr & 0x20)) : 0x13;
60 frame->faultAddress = 0;
61 frame->faultStatus = 0;
62 frame->vector = 0;
63 return frame;
64}