The Pedigree Project  0.1
x64/StackFrame.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/StackFrame.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/processor/state.h"
23 #include "pedigree/kernel/processor/types.h"
24 #include <stdarg.h>
25 
26 #if defined(DEBUGGER)
27 
28 uintptr_t X64StackFrame::getParameter(size_t n)
29 {
30  if (n == 0)
31  return m_State.rdi;
32  if (n == 1)
33  return m_State.rsi;
34  if (n == 2)
35  return m_State.rdx;
36  if (n == 3)
37  return m_State.rcx;
38  if (n == 4)
39  return m_State.r8;
40  if (n == 5)
41  return m_State.r9;
42 
43 #if defined(OMIT_FRAMEPOINTER)
44  uint64_t *pPtr = reinterpret_cast<uint64_t *>(
45  m_State.rbp + (n - 6 - 1) * sizeof(uint64_t));
46 #else
47  uint64_t *pPtr =
48  reinterpret_cast<uint64_t *>(m_State.rbp + (n - 6) * sizeof(uint64_t));
49 #endif
50  return *pPtr;
51 }
52 
53 #endif
54 
55 void X64StackFrame::construct(
56  ProcessorState &state, uintptr_t returnAddress, unsigned int nParams, ...)
57 {
58  // Align the stack to a 16-byte boundary. SysV AMD64 ABI supplement, Sn
59  // 3.2.2.
60  uintptr_t stack = state.getStackPointer();
61  if (stack & 0xF)
62  {
63  WARNING("StackFrame: given stack was not ABI-compliant, fixing.");
64  stack &= ~0xF;
65  }
66 
67  // Obtain the stack pointer.
68  uintptr_t *pStack = reinterpret_cast<uintptr_t *>(stack);
69 
70  // How many parameters do we need to push?
71  // We push in reverse order but must iterate through the va_list in forward
72  // order, so we decrement the stack pointer here.
73  ssize_t nToPush = static_cast<ssize_t>(nParams) -
74  6; // 6 Params can be passed in registers.
75 
76  if (nToPush < 0)
77  nToPush = 0;
78  nToPush++; // But we always have to push our return address.
79 
80  pStack -= nToPush;
81  uintptr_t *pStackLowWaterMark = pStack;
82 
83  *pStack++ = returnAddress;
84 
85  va_list list;
86  va_start(list, nParams);
87 
88  for (size_t i = 0; i < nParams; i++)
89  {
90  uintptr_t arg = va_arg(list, uintptr_t);
91  switch (i)
92  {
93  case 0:
94  state.rdi = arg;
95  break;
96  case 1:
97  state.rsi = arg;
98  break;
99  case 2:
100  state.rdx = arg;
101  break;
102  case 3:
103  state.rcx = arg;
104  break;
105  case 4:
106  state.r8 = arg;
107  break;
108  case 5:
109  state.r9 = arg;
110  break;
111  default:
112  *pStack++ = arg;
113  }
114  }
115 
116  va_end(list);
117 
118  // Write the new stack pointer back.
119  state.setStackPointer(reinterpret_cast<uintptr_t>(pStackLowWaterMark));
120 }
const ProcessorState & m_State
virtual uintptr_t getParameter(size_t n)
#define WARNING(text)
Definition: Log.h:78