The Pedigree Project  0.1
x64/state.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/processor/state.h"
21 #include "pedigree/kernel/processor/types.h"
22 
23 static const char *X64InterruptStateRegisterName[18] = {
24  "rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "r8", "r9",
25  "r10", "r11", "r12", "r13", "r14", "r15", "rsp", "rip", "rflags"};
26 
27 static const char *X64SyscallStateRegisterName[16] = {
28  "rax", "rbx", "rdx", "rdi", "rsi", "rbp", "r8", "r9",
29  "r10", "r12", "r13", "r14", "r15", "rsp", "rip", "rflags"};
30 
32 {
33  return 18;
34 }
35 processor_register_t X64InterruptState::getRegister(size_t index) const
36 {
37  if (index == 0)
38  return m_Rax;
39  if (index == 1)
40  return m_Rbx;
41  if (index == 2)
42  return m_Rcx;
43  if (index == 3)
44  return m_Rdx;
45  if (index == 4)
46  return m_Rdi;
47  if (index == 5)
48  return m_Rsi;
49  if (index == 6)
50  return m_Rbp;
51  if (index == 7)
52  return m_R8;
53  if (index == 8)
54  return m_R9;
55  if (index == 9)
56  return m_R10;
57  if (index == 10)
58  return m_R11;
59  if (index == 11)
60  return m_R12;
61  if (index == 12)
62  return m_R13;
63  if (index == 13)
64  return m_R14;
65  if (index == 14)
66  return m_R15;
67  if (index == 15)
68  return getStackPointer();
69  if (index == 16)
70  return m_Rip;
71  if (index == 17)
72  return m_Rflags;
73  return 0;
74 }
75 void X64InterruptState::setRegister(size_t index, uintptr_t value)
76 {
77  if (index == 0)
78  m_Rax = value;
79  if (index == 1)
80  m_Rbx = value;
81  if (index == 2)
82  m_Rcx = value;
83  if (index == 3)
84  m_Rdx = value;
85  if (index == 4)
86  m_Rdi = value;
87  if (index == 5)
88  m_Rsi = value;
89  if (index == 6)
90  m_Rbp = value;
91  if (index == 7)
92  m_R8 = value;
93  if (index == 8)
94  m_R9 = value;
95  if (index == 9)
96  m_R10 = value;
97  if (index == 10)
98  m_R11 = value;
99  if (index == 11)
100  m_R12 = value;
101  if (index == 12)
102  m_R13 = value;
103  if (index == 13)
104  m_R14 = value;
105  if (index == 14)
106  m_R15 = value;
107  if (index == 15)
108  m_Rsp = value;
109  if (index == 16)
110  m_Rip = value;
111  if (index == 17)
112  m_Rflags = value;
113 }
114 const char *X64InterruptState::getRegisterName(size_t index) const
115 {
116  return X64InterruptStateRegisterName[index];
117 }
118 
120 {
121  return 16;
122 }
123 processor_register_t X64SyscallState::getRegister(size_t index) const
124 {
125  if (index == 0)
126  return m_Rax;
127  if (index == 1)
128  return m_Rbx;
129  if (index == 2)
130  return m_Rdx;
131  if (index == 3)
132  return m_Rdi;
133  if (index == 4)
134  return m_Rsi;
135  if (index == 5)
136  return m_Rbp;
137  if (index == 6)
138  return m_R8;
139  if (index == 7)
140  return m_R9;
141  if (index == 8)
142  return m_R10;
143  if (index == 9)
144  return m_R12;
145  if (index == 10)
146  return m_R13;
147  if (index == 11)
148  return m_R14;
149  if (index == 12)
150  return m_R15;
151  if (index == 13)
152  return m_Rsp;
153  if (index == 14)
154  return m_RipRcx;
155  if (index == 15)
156  return m_RFlagsR11;
157  return 0;
158 }
159 const char *X64SyscallState::getRegisterName(size_t index) const
160 {
161  return X64SyscallStateRegisterName[index];
162 }
163 
166 {
167  // Obtain the stack pointer.
168  uintptr_t *pStack = reinterpret_cast<uintptr_t *>(state.getStackPointer());
169 
170  *--pStack = (userMode) ? 0x23 : 0x10; // SS
171  *--pStack = state.rsp; // RSP
172  *--pStack = 0x200; // RFLAGS - IF enabled.
173  *--pStack = (userMode) ? 0x1b : 0x08; // CS
174  *--pStack = state.rip; // RIP
175  *--pStack = 0; // Error code
176  *--pStack = 0; // Interrupt number
177  *--pStack = state.rax; // RAX
178  *--pStack = state.rbx; // RBX
179  *--pStack = state.rcx; // RCX
180  *--pStack = state.rdx; // RDX
181  *--pStack = state.rdi; // RDI
182  *--pStack = state.rsi; // RSI
183  *--pStack = state.rbp; // RBP
184  *--pStack = state.r8;
185  *--pStack = state.r9;
186  *--pStack = state.r10;
187  *--pStack = state.r11;
188  *--pStack = state.r12;
189  *--pStack = state.r13;
190  *--pStack = state.r14;
191  *--pStack = state.r15;
192 
193  X64InterruptState *toRet = reinterpret_cast<X64InterruptState *>(pStack);
194 
195  return toRet;
196 }
uint64_t m_R13
Definition: x64/state.h:127
uint64_t m_R14
Definition: x64/state.h:125
uint64_t m_Rax
Definition: x64/state.h:151
processor_register_t getRegister(size_t index) const
Definition: x64/state.cc:35
uint64_t m_Rcx
Definition: x64/state.h:147
uint64_t m_Rbx
Definition: x64/state.h:149
uint64_t m_R11
Definition: x64/state.h:131
uint64_t m_Rsp
Definition: x64/state.h:163
uint64_t m_Rsi
Definition: x64/state.h:141
void setRegister(size_t index, uintptr_t value)
Definition: x64/state.cc:75
processor_register_t getRegister(size_t index) const
Definition: x64/state.cc:123
static X64InterruptState * construct(class X64ProcessorState &state, bool userMode)
Definition: x64/state.cc:165
uint64_t m_R12
Definition: x64/state.h:129
size_t getRegisterCount() const
Definition: x64/state.cc:119
size_t getRegisterCount() const
Definition: x64/state.cc:31
uint64_t m_R15
Definition: x64/state.h:123
uint64_t m_Rdx
Definition: x64/state.h:145
const char * getRegisterName(size_t index) const
Definition: x64/state.cc:159
uint64_t m_Rip
Definition: x64/state.h:157
const char * getRegisterName(size_t index) const
Definition: x64/state.cc:114
uint64_t m_R10
Definition: x64/state.h:133
uint64_t m_Rbp
Definition: x64/state.h:139
uintptr_t getStackPointer() const
Definition: x64/state.h:381
uintptr_t getStackPointer() const
Definition: x64/state.h:614
uint64_t m_Rdi
Definition: x64/state.h:143
uint64_t m_Rflags
Definition: x64/state.h:161