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 
needs_setup() const44   bool needs_setup() const {
45     return _base == NULL;
46   }
47 
48   int suggest_size(Thread *thread) const;
49 
setup(void * mem,size_t size)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   }
teardown()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 
sp() const67   intptr_t *sp() const {
68     return _sp;
69   }
set_sp(intptr_t * new_sp)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 
total_words() const75   int total_words() const {
76     return _top - _base;
77   }
available_words() const78   int available_words() const {
79     return _sp - _base;
80   }
81 
push(intptr_t value)82   void push(intptr_t value) {
83     assert(_sp > _base, "stack overflow");
84     *(--_sp) = value;
85   }
pop()86   intptr_t pop() {
87     assert(_sp < _top, "stack underflow");
88     return *(_sp++);
89   }
90 
alloc(size_t size)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 
shadow_pages_size() const97   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:
base_offset()110   static ByteSize base_offset() {
111     return byte_offset_of(ZeroStack, _base);
112   }
top_offset()113   static ByteSize top_offset() {
114     return byte_offset_of(ZeroStack, _top);
115   }
sp_offset()116   static ByteSize sp_offset() {
117     return byte_offset_of(ZeroStack, _sp);
118   }
119 };
120 
121 
122 class EntryFrame;
123 class InterpreterFrame;
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:
ZeroFrame()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     FAKE_STUB_FRAME
154   };
155 
156  protected:
addr_of_word(int offset) const157   intptr_t *addr_of_word(int offset) const {
158     return (intptr_t *) this - offset;
159   }
value_of_word(int offset) const160   intptr_t value_of_word(int offset) const {
161     return *addr_of_word(offset);
162   }
163 
164  public:
next() const165   ZeroFrame *next() const {
166     return (ZeroFrame *) value_of_word(next_frame_off);
167   }
168 
169  protected:
type() const170   FrameType type() const {
171     return (FrameType) value_of_word(frame_type_off);
172   }
173 
174  public:
is_entry_frame() const175   bool is_entry_frame() const {
176     return type() == ENTRY_FRAME;
177   }
is_interpreter_frame() const178   bool is_interpreter_frame() const {
179     return type() == INTERPRETER_FRAME;
180   }
is_fake_stub_frame() const181   bool is_fake_stub_frame() const {
182     return type() == FAKE_STUB_FRAME;
183   }
184 
185  public:
as_entry_frame() const186   EntryFrame *as_entry_frame() const {
187     assert(is_entry_frame(), "should be");
188     return (EntryFrame *) this;
189   }
as_interpreter_frame() const190   InterpreterFrame *as_interpreter_frame() const {
191     assert(is_interpreter_frame(), "should be");
192     return (InterpreterFrame *) this;
193   }
as_fake_stub_frame() const194   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 };
214 
215 #endif // CPU_ZERO_VM_STACK_ZERO_HPP
216