The Pedigree Project  0.1
x86/state.h
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 #ifndef KERNEL_PROCESSOR_X86_STATE_H
21 #define KERNEL_PROCESSOR_X86_STATE_H
22 
23 #include "pedigree/kernel/Log.h"
24 #include "pedigree/kernel/compiler.h"
25 #include "pedigree/kernel/processor/types.h"
26 
32 {
33  friend class X86ProcessorState;
34  friend class X86InterruptManager;
35  friend class PageFaultHandler;
36 
37  public:
38  //
39  // General Interface (InterruptState, SyscallState & ProcessorState)
40  //
43  inline uintptr_t getStackPointer() const;
46  inline void setStackPointer(uintptr_t stackPointer);
50  inline uintptr_t getInstructionPointer() const;
53  inline void setInstructionPointer(uintptr_t instructionPointer);
56  inline uintptr_t getBasePointer() const;
59  inline void setBasePointer(uintptr_t basePointer);
60 
61  //
62  // General Interface (InterruptState & SyscallState)
63  //
66  size_t getRegisterCount() const;
70  processor_register_t getRegister(size_t index) const;
74  const char *getRegisterName(size_t index) const;
78  inline size_t getRegisterSize(size_t index) const;
79 
80  //
81  // InterruptState Interface
82  //
85  inline bool kernelMode() const;
88  inline size_t getInterruptNumber() const;
89 
90  //
91  // SyscallState Interface
92  //
95  inline size_t getSyscallService() const;
98  inline size_t getSyscallNumber() const;
100  inline uintptr_t getSyscallParameter(size_t n) const;
101  inline void setSyscallReturnValue(uintptr_t val);
102 
105  inline uint32_t getFlags() const;
108  inline void setFlags(uint32_t flags);
109 
112  static X86InterruptState *
113  construct(class X86ProcessorState &state, bool userMode);
114 
115  // private:
128 
130  uint32_t m_Ds;
132  uint32_t m_Edi;
134  uint32_t m_Esi;
136  uint32_t m_Ebp;
138  uint32_t m_Res;
140  uint32_t m_Ebx;
142  uint32_t m_Edx;
144  uint32_t m_Ecx;
146  uint32_t m_Eax;
148  uint32_t m_IntNumber;
150  uint32_t m_Errorcode;
152  uint32_t m_Eip;
154  uint32_t m_Cs;
156  uint32_t m_Eflags;
158  uint32_t m_Esp;
160  uint32_t m_Ss;
161 } PACKED;
162 
165 
168 {
169  public:
171  inline X86ProcessorState();
173  inline X86ProcessorState(const X86ProcessorState &x);
176  inline X86ProcessorState(const X86InterruptState &x);
183  inline ~X86ProcessorState();
184 
185  //
186  // General Interface (InterruptState, SyscallState & ProcessorState)
187  //
190  inline uintptr_t getStackPointer() const;
193  inline void setStackPointer(uintptr_t stackPointer);
197  inline uintptr_t getInstructionPointer() const;
200  inline void setInstructionPointer(uintptr_t instructionPointer);
203  inline uintptr_t getBasePointer() const;
206  inline void setBasePointer(uintptr_t basePointer);
207 
208  inline void setSyscallReturnValue(uintptr_t val);
209 
211  uint32_t edi;
213  uint32_t esi;
215  uint32_t ebp;
217  uint32_t ebx;
219  uint32_t edx;
221  uint32_t ecx;
223  uint32_t eax;
225  uint32_t eip;
227  uint32_t eflags;
229  uint32_t esp;
230 };
231 
233 class __attribute__((aligned(16))) X86SchedulerState
234 {
235  public:
236  uint32_t edi;
237  uint32_t esi;
238  uint32_t ebx;
239  uint32_t ebp;
240  uint32_t esp;
241  uint32_t eip;
242 
243  // bit 0: Has FPU
244  // bit 1: Used SSE
245  uint32_t flags;
246 
247  uint8_t x87FPU_MMX_XMM_MXCSR_State[512 + 16] __attribute__((aligned(16)));
248 } __attribute__((aligned(16)));
249 
252 //
253 // Part of the Implementation
254 //
256 {
257  if (kernelMode())
258  return (m_Res + 20);
259  return m_Esp;
260 }
261 void X86InterruptState::setStackPointer(uintptr_t stackPointer)
262 {
263  if (!kernelMode())
264  m_Esp = stackPointer;
265 }
267 {
268  return m_Eip;
269 }
270 void X86InterruptState::setInstructionPointer(uintptr_t instructionPointer)
271 {
272  m_Eip = instructionPointer;
273 }
275 {
276  return m_Ebp;
277 }
278 void X86InterruptState::setBasePointer(uintptr_t basePointer)
279 {
280  m_Ebp = basePointer;
281 }
282 size_t X86InterruptState::getRegisterSize(size_t index) const
283 {
284  return 4;
285 }
286 
288 {
289  return (m_Cs == 0x08);
290 }
292 {
293  return m_IntNumber;
294 }
295 
297 {
298  return ((m_Eax >> 16) & 0xFFFF);
299 }
301 {
302  return (m_Eax & 0xFFFF);
303 }
304 uintptr_t X86InterruptState::getSyscallParameter(size_t n) const
305 {
306  switch (n)
307  {
308  case 0:
309  return m_Ebx;
310  case 1:
311  return m_Ecx;
312  case 2:
313  return m_Edx;
314  case 3:
315  return m_Esi;
316  case 4:
317  return m_Edi;
318  default:
319  WARNING("Bad syscall parameter requested: " << Dec << n);
320  return 0;
321  }
322 }
323 void X86InterruptState::setSyscallReturnValue(uintptr_t val)
324 {
325  m_Eax = val;
326 }
327 
329 {
330  return m_Eflags;
331 }
332 void X86InterruptState::setFlags(uint32_t flags)
333 {
334  m_Eflags = flags;
335 }
336 
338  : edi(), esi(), ebp(), ebx(), edx(), ecx(), eax(), eip(), eflags(), esp()
339 {
340 }
342  : edi(x.edi), esi(x.esi), ebp(x.ebp), ebx(x.ebx), edx(x.edx), ecx(x.ecx),
343  eax(x.eax), eip(x.eip), eflags(x.eflags), esp(x.esp)
344 {
345 }
347  : edi(x.m_Edi), esi(x.m_Esi), ebp(x.m_Ebp), ebx(x.m_Ebx), edx(x.m_Edx),
348  ecx(x.m_Ecx), eax(x.m_Eax), eip(x.m_Eip), eflags(x.m_Eflags),
349  esp(x.getStackPointer())
350 {
351 }
353 {
354  edi = x.edi;
355  esi = x.esi;
356  ebp = x.ebp;
357  ebx = x.ebx;
358  edx = x.edx;
359  ecx = x.ecx;
360  eax = x.eax;
361  eip = x.eip;
362  eflags = x.eflags;
363  esp = x.esp;
364  return *this;
365 }
367 {
368  edi = x.m_Edi;
369  esi = x.m_Esi;
370  ebp = x.m_Ebp;
371  ebx = x.m_Ebx;
372  edx = x.m_Edx;
373  ecx = x.m_Ecx;
374  eax = x.m_Eax;
375  eip = x.m_Eip;
376  eflags = x.m_Eflags;
377  esp = x.getStackPointer();
378  return *this;
379 }
381 {
382 }
383 
385 {
386  return esp;
387 }
388 void X86ProcessorState::setStackPointer(uintptr_t stackPointer)
389 {
390  esp = stackPointer;
391 }
393 {
394  return eip;
395 }
396 void X86ProcessorState::setInstructionPointer(uintptr_t instructionPointer)
397 {
398  eip = instructionPointer;
399 }
401 {
402  return ebp;
403 }
404 void X86ProcessorState::setBasePointer(uintptr_t basePointer)
405 {
406  ebp = basePointer;
407 }
408 void X86ProcessorState::setSyscallReturnValue(uintptr_t val)
409 {
410  eax = val;
411 }
412 
413 #endif
class __attribute__((aligned(16))) X86SchedulerState
Definition: x86/state.h:233
void setStackPointer(uintptr_t stackPointer)
Definition: x86/state.h:388
uint32_t m_Edi
Definition: x86/state.h:132
processor_register_t getRegister(size_t index) const
Definition: x86/state.cc:29
void setBasePointer(uintptr_t basePointer)
Definition: x86/state.h:404
void setInstructionPointer(uintptr_t instructionPointer)
Definition: x86/state.h:270
uint32_t m_Ebx
Definition: x86/state.h:140
size_t getRegisterCount() const
Definition: x86/state.cc:25
static X86InterruptState * construct(class X86ProcessorState &state, bool userMode)
Definition: x86/state.cc:59
X86ProcessorState & operator=(const X86ProcessorState &x)
Definition: x86/state.h:352
size_t getSyscallNumber() const
Definition: x86/state.h:300
uint32_t m_Esi
Definition: x86/state.h:134
uintptr_t getInstructionPointer() const
Definition: x86/state.h:266
void setFlags(uint32_t flags)
Definition: x86/state.h:332
size_t getInterruptNumber() const
Definition: x86/state.h:291
uint32_t m_IntNumber
Definition: x86/state.h:148
void setInstructionPointer(uintptr_t instructionPointer)
Definition: x86/state.h:396
void setStackPointer(uintptr_t stackPointer)
Definition: x86/state.h:261
uint32_t m_Edx
Definition: x86/state.h:142
#define WARNING(text)
Definition: Log.h:78
X86InterruptState & operator=(const X86InterruptState &)
uint32_t m_Eip
Definition: x86/state.h:152
uintptr_t getSyscallParameter(size_t n) const
Definition: x86/state.h:304
uint32_t m_Esp
Definition: x86/state.h:158
uint32_t m_Errorcode
Definition: x86/state.h:150
uint32_t getFlags() const
Definition: x86/state.h:328
uintptr_t getBasePointer() const
Definition: x86/state.h:274
X86InterruptState X86SyscallState
Definition: x86/state.h:164
uintptr_t getInstructionPointer() const
Definition: x86/state.h:392
uint32_t m_Eax
Definition: x86/state.h:146
uintptr_t getStackPointer() const
Definition: x86/state.h:255
uintptr_t getBasePointer() const
Definition: x86/state.h:400
uint32_t m_Eflags
Definition: x86/state.h:156
uint32_t eflags
Definition: x86/state.h:227
const char * getRegisterName(size_t index) const
Definition: x86/state.cc:53
size_t getRegisterSize(size_t index) const
Definition: x86/state.h:282
Definition: Log.h:138
bool kernelMode() const
Definition: x86/state.h:287
void setBasePointer(uintptr_t basePointer)
Definition: x86/state.h:278
uintptr_t getStackPointer() const
Definition: x86/state.h:384
uint32_t m_Res
Definition: x86/state.h:138
size_t getSyscallService() const
Definition: x86/state.h:296
uint32_t m_Ebp
Definition: x86/state.h:136
uint32_t m_Ecx
Definition: x86/state.h:144