The Pedigree Project  0.1
x64/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_X64_STATE_H
21 #define KERNEL_PROCESSOR_X64_STATE_H
22 
23 // IWYU pragma: private, include "pedigree/kernel/processor/state.h"
24 
25 #include "pedigree/kernel/Log.h"
26 #include "pedigree/kernel/compiler.h"
27 #include "pedigree/kernel/processor/types.h"
28 
34 {
35  friend class X64ProcessorState;
36  friend class X64InterruptManager;
37  friend class PageFaultHandler;
38 
39  public:
40  //
41  // General Interface (InterruptState, SyscallState & ProcessorState)
42  //
45  inline uintptr_t getStackPointer() const;
48  inline void setStackPointer(uintptr_t stackPointer);
52  inline uintptr_t getInstructionPointer() const;
55  inline void setInstructionPointer(uintptr_t instructionPointer);
58  inline uintptr_t getBasePointer() const;
61  inline void setBasePointer(uintptr_t basePointer);
62 
63  //
64  // General Interface (InterruptState & SyscallState)
65  //
68  size_t getRegisterCount() const;
72  processor_register_t getRegister(size_t index) const;
76  void setRegister(size_t index, uintptr_t value);
80  const char *getRegisterName(size_t index) const;
84  inline size_t getRegisterSize(size_t index) const;
85 
86  //
87  // InterruptState Interface
88  //
91  inline bool kernelMode() const;
94  inline size_t getInterruptNumber() const;
95 
98  inline uint64_t getFlags() const;
101  inline void setFlags(uint64_t flags);
102 
105  static X64InterruptState *
106  construct(class X64ProcessorState &state, bool userMode);
107 
108  private:
121 
123  uint64_t m_R15;
125  uint64_t m_R14;
127  uint64_t m_R13;
129  uint64_t m_R12;
131  uint64_t m_R11;
133  uint64_t m_R10;
135  uint64_t m_R9;
137  uint64_t m_R8;
139  uint64_t m_Rbp;
141  uint64_t m_Rsi;
143  uint64_t m_Rdi;
145  uint64_t m_Rdx;
147  uint64_t m_Rcx;
149  uint64_t m_Rbx;
151  uint64_t m_Rax;
153  uint64_t m_IntNumber;
155  uint64_t m_Errorcode;
157  uint64_t m_Rip;
159  uint64_t m_Cs;
161  uint64_t m_Rflags;
163  uint64_t m_Rsp;
165  uint64_t m_Ss;
166 } PACKED;
167 
170 {
171  friend class X64ProcessorState;
172  friend class X64SyscallManager;
173 
174  public:
175  //
176  // General Interface (InterruptState, SyscallState & ProcessorState)
177  //
180  inline uintptr_t getStackPointer() const;
183  inline void setStackPointer(uintptr_t stackPointer);
187  inline uintptr_t getInstructionPointer() const;
190  inline void setInstructionPointer(uintptr_t instructionPointer);
193  inline uintptr_t getBasePointer() const;
196  inline void setBasePointer(uintptr_t basePointer);
197 
198  //
199  // General Interface (InterruptState & SyscallState)
200  //
203  size_t getRegisterCount() const;
207  processor_register_t getRegister(size_t index) const;
211  const char *getRegisterName(size_t index) const;
215  inline size_t getRegisterSize(size_t index) const;
216 
217  //
218  // SyscallState Interface
219  //
222  inline size_t getSyscallService() const;
225  inline size_t getSyscallNumber() const;
227  inline uintptr_t getSyscallParameter(size_t n) const;
228  inline void setSyscallReturnValue(uintptr_t val);
229  inline void setSyscallErrno(uintptr_t val);
230 
231  inline uint64_t getFlags() const;
232  inline void setFlags(uint64_t newFlags);
233 
234  public:
236  uint64_t m_R15;
238  uint64_t m_R14;
240  uint64_t m_R13;
242  uint64_t m_R12;
244  uint64_t m_R10;
246  uint64_t m_R9;
248  uint64_t m_R8;
250  uint64_t m_Rbp;
251 
252  public:
254  uint64_t m_Rsi;
256  uint64_t m_Rdi;
257 
258  private:
260  uint64_t m_Rdx;
262  uint64_t m_Rbx;
264  uint64_t m_Rax;
266  uint64_t m_RFlagsR11;
268  uint64_t m_RipRcx;
270  uint64_t m_Rsp;
271 } PACKED;
272 
275 {
276  public:
278  inline X64ProcessorState();
280  inline X64ProcessorState(const X64ProcessorState &);
283  inline X64ProcessorState(const X64InterruptState &);
286  inline X64ProcessorState(const X64SyscallState &);
296  inline ~X64ProcessorState();
297 
298  //
299  // General Interface (InterruptState, SyscallState & ProcessorState)
300  //
303  inline uintptr_t getStackPointer() const;
306  inline void setStackPointer(uintptr_t stackPointer);
310  inline uintptr_t getInstructionPointer() const;
313  inline void setInstructionPointer(uintptr_t instructionPointer);
316  inline uintptr_t getBasePointer() const;
319  inline void setBasePointer(uintptr_t basePointer);
320 
322  uint64_t r15;
324  uint64_t r14;
326  uint64_t r13;
328  uint64_t r12;
330  uint64_t r11;
332  uint64_t r10;
334  uint64_t r9;
336  uint64_t r8;
338  uint64_t rbp;
340  uint64_t rsi;
342  uint64_t rdi;
344  uint64_t rdx;
346  uint64_t rcx;
348  uint64_t rbx;
350  uint64_t rax;
352  uint64_t rip;
354  uint64_t rflags;
356  uint64_t rsp;
357 };
358 
361 {
362  public:
363  uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
364  uint64_t rbx;
365  uint64_t rbp;
366  uint64_t rsp;
367  uint64_t rip;
368 
369  // bit 0: Has FPU
370  // bit 1: Used SSE
371  uint32_t flags;
372 
373  uint8_t x87FPU_MMX_XMM_MXCSR_State[512 + 16] __attribute__((aligned(16)));
374 };
375 
378 //
379 // Part of the Implementation
380 //
382 {
383  return m_Rsp;
384 }
385 void X64InterruptState::setStackPointer(uintptr_t stackPointer)
386 {
387  m_Rsp = stackPointer;
388 }
390 {
391  return m_Rip;
392 }
393 void X64InterruptState::setInstructionPointer(uintptr_t instructionPointer)
394 {
395  m_Rip = instructionPointer;
396 }
398 {
399  return m_Rbp;
400 }
401 void X64InterruptState::setBasePointer(uintptr_t basePointer)
402 {
403  m_Rbp = basePointer;
404 }
405 size_t X64InterruptState::getRegisterSize(size_t index) const
406 {
407  return 8;
408 }
409 
411 {
412  return (m_Cs == 0x08);
413 }
415 {
416  return m_IntNumber;
417 }
418 
420 {
421  return m_Rflags;
422 }
423 void X64InterruptState::setFlags(uint64_t flags)
424 {
425  m_Rflags = flags;
426 }
427 
429 {
430  return m_Rsp;
431 }
432 void X64SyscallState::setStackPointer(uintptr_t stackPointer)
433 {
434  m_Rsp = stackPointer;
435 }
437 {
438  return m_RipRcx;
439 }
440 void X64SyscallState::setInstructionPointer(uintptr_t instructionPointer)
441 {
442  m_RipRcx = instructionPointer;
443 }
445 {
446  return m_Rbp;
447 }
448 void X64SyscallState::setBasePointer(uintptr_t basePointer)
449 {
450  m_Rbp = basePointer;
451 }
452 size_t X64SyscallState::getRegisterSize(size_t index) const
453 {
454  return 8;
455 }
456 
458 {
459  return ((m_Rax >> 16) & 0xFFFF);
460 }
462 {
463  return (m_Rax & 0xFFFF);
464 }
465 uintptr_t X64SyscallState::getSyscallParameter(size_t n) const
466 {
467  switch (n)
468  {
469  case 0:
470  return m_Rbx;
471  case 1:
472  return m_Rdx;
473  case 2:
474  return m_Rsi;
475  case 3:
476  return m_Rdi;
477  case 4:
478  return m_R8;
479  case 5:
480  return m_R9;
481 
482  // redirection for linux syscall parameters
483  case 6:
484  return m_Rdi;
485  case 7:
486  return m_Rsi;
487  case 8:
488  return m_Rdx;
489  case 9:
490  return m_R10;
491  case 10:
492  return m_R8;
493  case 11:
494  return m_R9;
495 
496  default:
497  WARNING("Bad syscall parameter requested: " << Dec << n);
498  return 0;
499  }
500 }
501 void X64SyscallState::setSyscallReturnValue(uintptr_t val)
502 {
503  m_Rax = val;
504 }
505 void X64SyscallState::setSyscallErrno(uintptr_t val)
506 {
507  m_Rbx = val;
508 }
509 uint64_t X64SyscallState::getFlags() const
510 {
511  return m_RFlagsR11;
512 }
513 void X64SyscallState::setFlags(uint64_t newFlags)
514 {
515  m_RFlagsR11 = newFlags;
516 }
517 
519  : r15(), r14(), r13(), r12(), r11(), r10(), r9(), r8(), rbp(), rsi(), rdi(),
520  rdx(), rcx(), rbx(), rax(), rip(), rflags(), rsp()
521 {
522 }
524  : r15(x.r15), r14(x.r14), r13(x.r13), r12(x.r12), r11(x.r11), r10(x.r10),
525  r9(x.r9), r8(x.r8), rbp(x.rbp), rsi(x.rsi), rdi(x.rdi), rdx(x.rdx),
526  rcx(x.rcx), rbx(x.rbx), rax(x.rax), rip(x.rip), rflags(x.rflags),
527  rsp(x.rsp)
528 {
529 }
531  : r15(x.m_R15), r14(x.m_R14), r13(x.m_R13), r12(x.m_R12), r11(x.m_R11),
532  r10(x.m_R10), r9(x.m_R9), r8(x.m_R8), rbp(x.m_Rbp), rsi(x.m_Rsi),
533  rdi(x.m_Rdi), rdx(x.m_Rdx), rcx(x.m_Rcx), rbx(x.m_Rbx), rax(x.m_Rax),
534  rip(x.m_Rip), rflags(x.m_Rflags), rsp(x.m_Rsp)
535 {
536 }
538  : r15(x.m_R15), r14(x.m_R14), r13(x.m_R13), r12(x.m_R12),
539  r11(x.m_RFlagsR11), r10(x.m_R10), r9(x.m_R9), r8(x.m_R8), rbp(x.m_Rbp),
540  rsi(x.m_Rsi), rdi(x.m_Rdi), rdx(x.m_Rdx), rcx(x.m_RipRcx), rbx(x.m_Rbx),
541  rax(x.m_Rax), rip(x.m_RipRcx), rflags(x.m_RFlagsR11), rsp(x.m_Rsp)
542 {
543 }
545 {
546  r15 = x.r15;
547  r14 = x.r14;
548  r13 = x.r13;
549  r12 = x.r12;
550  r11 = x.r11;
551  r10 = x.r10;
552  r9 = x.r9;
553  r8 = x.r8;
554  rbp = x.rbp;
555  rsi = x.rsi;
556  rdi = x.rdi;
557  rdx = x.rdx;
558  rcx = x.rcx;
559  rbx = x.rbx;
560  rax = x.rax;
561  rip = x.rip;
562  rflags = x.rflags;
563  rsp = x.rsp;
564  return *this;
565 }
567 {
568  r15 = x.m_R15;
569  r14 = x.m_R14;
570  r13 = x.m_R13;
571  r12 = x.m_R12;
572  r11 = x.m_R11;
573  r10 = x.m_R10;
574  r9 = x.m_R9;
575  r8 = x.m_R8;
576  rbp = x.m_Rbp;
577  rsi = x.m_Rsi;
578  rdi = x.m_Rdi;
579  rdx = x.m_Rdx;
580  rcx = x.m_Rcx;
581  rbx = x.m_Rbx;
582  rax = x.m_Rax;
583  rip = x.m_Rip;
584  rflags = x.m_Rflags;
585  rsp = x.m_Rsp;
586  return *this;
587 }
589 {
590  r15 = x.m_R15;
591  r14 = x.m_R14;
592  r13 = x.m_R13;
593  r12 = x.m_R12;
594  r11 = x.m_RFlagsR11;
595  r10 = x.m_R10;
596  r9 = x.m_R9;
597  r8 = x.m_R8;
598  rbp = x.m_Rbp;
599  rsi = x.m_Rsi;
600  rdi = x.m_Rdi;
601  rdx = x.m_Rdx;
602  rcx = x.m_RipRcx;
603  rbx = x.m_Rbx;
604  rax = x.m_Rax;
605  rip = x.m_RipRcx;
606  rflags = x.m_RFlagsR11;
607  rsp = x.m_Rsp;
608  return *this;
609 }
611 {
612 }
613 
615 {
616  return rsp;
617 }
618 void X64ProcessorState::setStackPointer(uintptr_t stackPointer)
619 {
620  rsp = stackPointer;
621 }
623 {
624  return rip;
625 }
626 void X64ProcessorState::setInstructionPointer(uintptr_t instructionPointer)
627 {
628  rip = instructionPointer;
629 }
631 {
632  return rbp;
633 }
634 void X64ProcessorState::setBasePointer(uintptr_t basePointer)
635 {
636  rbp = basePointer;
637 }
638 
639 #endif
uint64_t m_Errorcode
Definition: x64/state.h:155
X64ProcessorState & operator=(const X64ProcessorState &)
Definition: x64/state.h:544
size_t getSyscallNumber() const
Definition: x64/state.h:461
void setStackPointer(uintptr_t stackPointer)
Definition: x64/state.h:385
uint64_t m_R13
Definition: x64/state.h:127
uint64_t m_R14
Definition: x64/state.h:125
bool kernelMode() const
Definition: x64/state.h:410
uint64_t getFlags() const
Definition: x64/state.h:419
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
uintptr_t getInstructionPointer() const
Definition: x64/state.h:389
void setBasePointer(uintptr_t basePointer)
Definition: x64/state.h:448
uint64_t m_Rbx
Definition: x64/state.h:149
uint64_t m_R11
Definition: x64/state.h:131
void setFlags(uint64_t flags)
Definition: x64/state.h:423
uint64_t m_Rsp
Definition: x64/state.h:163
size_t getInterruptNumber() const
Definition: x64/state.h:414
uint64_t m_Rdx
Definition: x64/state.h:260
uint64_t m_Rsi
Definition: x64/state.h:141
uintptr_t getInstructionPointer() const
Definition: x64/state.h:436
void setRegister(size_t index, uintptr_t value)
Definition: x64/state.cc:75
uintptr_t getBasePointer() const
Definition: x64/state.h:444
uint64_t m_Rdi
Definition: x64/state.h:256
static X64InterruptState * construct(class X64ProcessorState &state, bool userMode)
Definition: x64/state.cc:165
uintptr_t getInstructionPointer() const
Definition: x64/state.h:622
uint64_t m_R8
Definition: x64/state.h:248
uint64_t m_R12
Definition: x64/state.h:129
uint64_t m_R14
Definition: x64/state.h:238
void setInstructionPointer(uintptr_t instructionPointer)
Definition: x64/state.h:440
uint64_t m_Rsi
Definition: x64/state.h:254
uint64_t m_RipRcx
Definition: x64/state.h:268
uintptr_t getStackPointer() const
Definition: x64/state.h:428
uint64_t m_R12
Definition: x64/state.h:242
size_t getRegisterCount() const
Definition: x64/state.cc:31
#define WARNING(text)
Definition: Log.h:78
uint64_t m_RFlagsR11
Definition: x64/state.h:266
size_t getRegisterSize(size_t index) const
Definition: x64/state.h:452
X64InterruptState & operator=(const X64InterruptState &)
uint64_t m_R15
Definition: x64/state.h:123
uint64_t m_Rbp
Definition: x64/state.h:250
uint64_t m_Rdx
Definition: x64/state.h:145
uint64_t m_R15
Definition: x64/state.h:236
uint64_t m_Rax
Definition: x64/state.h:264
size_t getSyscallService() const
Definition: x64/state.h:457
uint64_t m_Rip
Definition: x64/state.h:157
uint64_t m_Rsp
Definition: x64/state.h:270
const char * getRegisterName(size_t index) const
Definition: x64/state.cc:114
uintptr_t getSyscallParameter(size_t n) const
Definition: x64/state.h:465
size_t getRegisterSize(size_t index) const
Definition: x64/state.h:405
void setBasePointer(uintptr_t basePointer)
Definition: x64/state.h:401
void setInstructionPointer(uintptr_t instructionPointer)
Definition: x64/state.h:393
uint64_t m_R10
Definition: x64/state.h:244
uint64_t m_R10
Definition: x64/state.h:133
uint64_t m_Rbp
Definition: x64/state.h:139
uintptr_t getBasePointer() const
Definition: x64/state.h:397
uint64_t m_R9
Definition: x64/state.h:246
Definition: Log.h:138
uintptr_t getBasePointer() const
Definition: x64/state.h:630
uintptr_t getStackPointer() const
Definition: x64/state.h:381
uint64_t m_Rbx
Definition: x64/state.h:262
uint64_t m_IntNumber
Definition: x64/state.h:153
void setStackPointer(uintptr_t stackPointer)
Definition: x64/state.h:618
void setBasePointer(uintptr_t basePointer)
Definition: x64/state.h:634
uint64_t rflags
Definition: x64/state.h:354
uintptr_t getStackPointer() const
Definition: x64/state.h:614
void setInstructionPointer(uintptr_t instructionPointer)
Definition: x64/state.h:626
uint64_t m_Rdi
Definition: x64/state.h:143
void setStackPointer(uintptr_t stackPointer)
Definition: x64/state.h:432
uint64_t m_R13
Definition: x64/state.h:240
uint64_t m_Rflags
Definition: x64/state.h:161