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