1 /*
   2  * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
   3  * Copyright 2008, 2009, 2010 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21  * CA 95054 USA or visit www.sun.com if you need additional information or
  22  * have any questions.
  23  *
  24  */
  25 
  26 class ZeroStack {
  27  private:
  28   intptr_t *_base; // the last available word
  29   intptr_t *_top;  // the word past the end of the stack
  30   intptr_t *_sp;   // the top word on the stack
  31 
  32  private:
  33   int _shadow_pages_size; // how much ABI stack must we keep free?
  34 
  35  public:
  36   ZeroStack()
  37     : _base(NULL), _top(NULL), _sp(NULL) {
  38     _shadow_pages_size = StackShadowPages * os::vm_page_size();
  39   }
  40 
  41   bool needs_setup() const {
  42     return _base == NULL;
  43   }
  44 
  45   void setup(void *mem, size_t size) {
  46     assert(needs_setup(), "already set up");
  47     assert(!(size & WordAlignmentMask), "unaligned");
  48 
  49     _base = (intptr_t *) mem;
  50     _top  = _base + (size >> LogBytesPerWord);
  51     _sp   = _top;
  52   }
  53   void teardown() {
  54     assert(!needs_setup(), "not set up");
  55     assert(_sp == _top, "stuff on stack at teardown");
  56 
  57     _base = NULL;
  58     _top  = NULL;
  59     _sp   = NULL;
  60   }
  61 
  62   intptr_t *sp() const {
  63     return _sp;
  64   }
  65   void set_sp(intptr_t *new_sp) {
  66     assert(_top >= new_sp && new_sp >= _base, "bad stack pointer");
  67     _sp = new_sp;
  68   }
  69 
  70   int available_words() const {
  71     return _sp - _base;
  72   }
  73 
  74   void push(intptr_t value) {
  75     assert(_sp > _base, "stack overflow");
  76     *(--_sp) = value;
  77   }
  78   intptr_t pop() {
  79     assert(_sp < _top, "stack underflow");
  80     return *(_sp++);
  81   }
  82 
  83   void *alloc(size_t size) {
  84     int count = align_size_up(size, wordSize) >> LogBytesPerWord;
  85     assert(count <= available_words(), "stack overflow");
  86     return _sp -= count;
  87   }
  88 
  89   int shadow_pages_size() const {
  90     return _shadow_pages_size;
  91   }
  92 
  93  public:
  94   void overflow_check(int required_words, TRAPS);
  95   static void handle_overflow(TRAPS);
  96 
  97  public:
  98   void zap(int c) PRODUCT_RETURN;
  99 
 100  public:
 101   static ByteSize base_offset() {
 102     return byte_offset_of(ZeroStack, _base);
 103   }
 104   static ByteSize top_offset() {
 105     return byte_offset_of(ZeroStack, _top);
 106   }
 107   static ByteSize sp_offset() {
 108     return byte_offset_of(ZeroStack, _sp);
 109   }
 110 };
 111 
 112 
 113 class EntryFrame;
 114 class InterpreterFrame;
 115 class SharkFrame;
 116 class FakeStubFrame;
 117 
 118 //
 119 // |  ...               |
 120 // +--------------------+  ------------------
 121 // |  ...               |       low addresses
 122 // | frame_type         |
 123 // | next_frame         |      high addresses
 124 // +--------------------+  ------------------
 125 // |  ...               |
 126 
 127 class ZeroFrame {
 128   friend class frame;
 129   friend class ZeroStackPrinter;
 130 
 131  protected:
 132   ZeroFrame() {
 133     ShouldNotCallThis();
 134   }
 135 
 136   enum Layout {
 137     next_frame_off,
 138     frame_type_off,
 139     jf_header_words
 140   };
 141 
 142   enum FrameType {
 143     ENTRY_FRAME = 1,
 144     INTERPRETER_FRAME,
 145     SHARK_FRAME,
 146     FAKE_STUB_FRAME
 147   };
 148 
 149  protected:
 150   intptr_t *addr_of_word(int offset) const {
 151     return (intptr_t *) this - offset;
 152   }
 153   intptr_t value_of_word(int offset) const {
 154     return *addr_of_word(offset);
 155   }
 156 
 157  public:
 158   ZeroFrame *next() const {
 159     return (ZeroFrame *) value_of_word(next_frame_off);
 160   }
 161 
 162  protected:
 163   FrameType type() const {
 164     return (FrameType) value_of_word(frame_type_off);
 165   }
 166 
 167  public:
 168   bool is_entry_frame() const {
 169     return type() == ENTRY_FRAME;
 170   }
 171   bool is_interpreter_frame() const {
 172     return type() == INTERPRETER_FRAME;
 173   }
 174   bool is_shark_frame() const {
 175     return type() == SHARK_FRAME;
 176   }
 177   bool is_fake_stub_frame() const {
 178     return type() == FAKE_STUB_FRAME;
 179   }
 180 
 181  public:
 182   EntryFrame *as_entry_frame() const {
 183     assert(is_entry_frame(), "should be");
 184     return (EntryFrame *) this;
 185   }
 186   InterpreterFrame *as_interpreter_frame() const {
 187     assert(is_interpreter_frame(), "should be");
 188     return (InterpreterFrame *) this;
 189   }
 190   SharkFrame *as_shark_frame() const {
 191     assert(is_shark_frame(), "should be");
 192     return (SharkFrame *) this;
 193   }
 194   FakeStubFrame *as_fake_stub_frame() const {
 195     assert(is_fake_stub_frame(), "should be");
 196     return (FakeStubFrame *) this;
 197   }
 198 
 199  public:
 200   void identify_word(int   frame_index,
 201                      int   offset,
 202                      char* fieldbuf,
 203                      char* valuebuf,
 204                      int   buflen) const;
 205 
 206  protected:
 207   void identify_vp_word(int       frame_index,
 208                         intptr_t* addr,
 209                         intptr_t* monitor_base,
 210                         intptr_t* stack_base,
 211                         char*     fieldbuf,
 212                         int       buflen) const;
 213 };