1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef CPU_ZERO_VM_STACK_ZERO_HPP
  27 #define CPU_ZERO_VM_STACK_ZERO_HPP
  28 
  29 #include "utilities/align.hpp"
  30 #include "utilities/sizes.hpp"
  31 
  32 class ZeroStack {
  33  private:
  34   intptr_t *_base; // the last available word
  35   intptr_t *_top;  // the word past the end of the stack
  36   intptr_t *_sp;   // the top word on the stack
  37 
  38  private:
  39   int _shadow_pages_size; // how much ABI stack must we keep free?
  40 
  41  public:
  42   ZeroStack();
  43 
  44   bool needs_setup() const {
  45     return _base == NULL;
  46   }
  47 
  48   int suggest_size(Thread *thread) const;
  49 
  50   void setup(void *mem, size_t size) {
  51     assert(needs_setup(), "already set up");
  52     assert(!(size & WordAlignmentMask), "unaligned");
  53 
  54     _base = (intptr_t *) mem;
  55     _top  = _base + (size >> LogBytesPerWord);
  56     _sp   = _top;
  57   }
  58   void teardown() {
  59     assert(!needs_setup(), "not set up");
  60     assert(_sp == _top, "stuff on stack at teardown");
  61 
  62     _base = NULL;
  63     _top  = NULL;
  64     _sp   = NULL;
  65   }
  66 
  67   intptr_t *sp() const {
  68     return _sp;
  69   }
  70   void set_sp(intptr_t *new_sp) {
  71     assert(_top >= new_sp && new_sp >= _base, "bad stack pointer");
  72     _sp = new_sp;
  73   }
  74 
  75   int total_words() const {
  76     return _top - _base;
  77   }
  78   int available_words() const {
  79     return _sp - _base;
  80   }
  81 
  82   void push(intptr_t value) {
  83     assert(_sp > _base, "stack overflow");
  84     *(--_sp) = value;
  85   }
  86   intptr_t pop() {
  87     assert(_sp < _top, "stack underflow");
  88     return *(_sp++);
  89   }
  90 
  91   void *alloc(size_t size) {
  92     int count = align_up(size, wordSize) >> LogBytesPerWord;
  93     assert(count <= available_words(), "stack overflow");
  94     return _sp -= count;
  95   }
  96 
  97   int shadow_pages_size() const {
  98     return _shadow_pages_size;
  99   }
 100   int abi_stack_available(Thread *thread) const;
 101 
 102  public:
 103   void overflow_check(int required_words, TRAPS);
 104   static void handle_overflow(TRAPS);
 105 
 106  public:
 107   void zap(int c) PRODUCT_RETURN;
 108 
 109  public:
 110   static ByteSize base_offset() {
 111     return byte_offset_of(ZeroStack, _base);
 112   }
 113   static ByteSize top_offset() {
 114     return byte_offset_of(ZeroStack, _top);
 115   }
 116   static ByteSize sp_offset() {
 117     return byte_offset_of(ZeroStack, _sp);
 118   }
 119 };
 120 
 121 
 122 class EntryFrame;
 123 class InterpreterFrame;
 124 class SharkFrame;
 125 class FakeStubFrame;
 126 
 127 //
 128 // |  ...               |
 129 // +--------------------+  ------------------
 130 // |  ...               |       low addresses
 131 // | frame_type         |
 132 // | next_frame         |      high addresses
 133 // +--------------------+  ------------------
 134 // |  ...               |
 135 
 136 class ZeroFrame {
 137   friend class frame;
 138   friend class ZeroStackPrinter;
 139 
 140  protected:
 141   ZeroFrame() {
 142     ShouldNotCallThis();
 143   }
 144 
 145   enum Layout {
 146     next_frame_off,
 147     frame_type_off,
 148     jf_header_words
 149   };
 150 
 151   enum FrameType {
 152     ENTRY_FRAME = 1,
 153     INTERPRETER_FRAME,
 154     SHARK_FRAME,
 155     FAKE_STUB_FRAME
 156   };
 157 
 158  protected:
 159   intptr_t *addr_of_word(int offset) const {
 160     return (intptr_t *) this - offset;
 161   }
 162   intptr_t value_of_word(int offset) const {
 163     return *addr_of_word(offset);
 164   }
 165 
 166  public:
 167   ZeroFrame *next() const {
 168     return (ZeroFrame *) value_of_word(next_frame_off);
 169   }
 170 
 171  protected:
 172   FrameType type() const {
 173     return (FrameType) value_of_word(frame_type_off);
 174   }
 175 
 176  public:
 177   bool is_entry_frame() const {
 178     return type() == ENTRY_FRAME;
 179   }
 180   bool is_interpreter_frame() const {
 181     return type() == INTERPRETER_FRAME;
 182   }
 183   bool is_shark_frame() const {
 184     return type() == SHARK_FRAME;
 185   }
 186   bool is_fake_stub_frame() const {
 187     return type() == FAKE_STUB_FRAME;
 188   }
 189 
 190  public:
 191   EntryFrame *as_entry_frame() const {
 192     assert(is_entry_frame(), "should be");
 193     return (EntryFrame *) this;
 194   }
 195   InterpreterFrame *as_interpreter_frame() const {
 196     assert(is_interpreter_frame(), "should be");
 197     return (InterpreterFrame *) this;
 198   }
 199   SharkFrame *as_shark_frame() const {
 200     assert(is_shark_frame(), "should be");
 201     return (SharkFrame *) this;
 202   }
 203   FakeStubFrame *as_fake_stub_frame() const {
 204     assert(is_fake_stub_frame(), "should be");
 205     return (FakeStubFrame *) this;
 206   }
 207 
 208  public:
 209   void identify_word(int   frame_index,
 210                      int   offset,
 211                      char* fieldbuf,
 212                      char* valuebuf,
 213                      int   buflen) const;
 214 
 215  protected:
 216   void identify_vp_word(int       frame_index,
 217                         intptr_t* addr,
 218                         intptr_t* monitor_base,
 219                         intptr_t* stack_base,
 220                         char*     fieldbuf,
 221                         int       buflen) const;
 222 };
 223 
 224 #endif // CPU_ZERO_VM_STACK_ZERO_HPP