The Pedigree Project  0.1
x86/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 <stdarg.h>
23 
24 #ifdef DEBUGGER
25 uintptr_t X86StackFrame::getParameter(size_t n)
26 {
29  if (m_BasePointer < 0x2000)
30  return 0;
31 #if defined(OMIT_FRAMEPOINTER)
32  uint32_t *pPtr = reinterpret_cast<uint32_t *>(
33  m_BasePointer + (n + 1) * sizeof(uint32_t));
34 #else
35  uint32_t *pPtr = reinterpret_cast<uint32_t *>(
36  m_BasePointer + (n + 2) * sizeof(uint32_t));
37 #endif
38  return *pPtr;
39 }
40 #endif
41 
42 void X86StackFrame::construct(
43  ProcessorState &state, uintptr_t returnAddress, unsigned int nParams, ...)
44 {
45  // Obtain the stack pointer.
46  uintptr_t *pStack = reinterpret_cast<uintptr_t *>(state.getStackPointer());
47 
48  // How many parameters do we need to push?
49  // We push in reverse order but must iterate through the va_list in forward
50  // order, so we decrement the stack pointer here.
51  pStack -= nParams + 1; // +1 for return address.
52  uintptr_t *pStackLowWaterMark = pStack;
53 
54  *pStack++ = returnAddress;
55 
56  va_list list;
57  va_start(list, nParams);
58 
59  for (int i = nParams - 1; i >= 0; i--)
60  {
61  *pStack++ = va_arg(list, uintptr_t);
62  }
63 
64  va_end(list);
65 
66  // Write the new stack pointer back.
67  state.setStackPointer(reinterpret_cast<uintptr_t>(pStackLowWaterMark));
68 }
virtual uintptr_t getParameter(size_t n)
uintptr_t m_BasePointer