frame_sparc.cpp revision 6760:22b98ab2a69f
1184610Salfred/*
2184610Salfred * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3184610Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4184610Salfred *
5184610Salfred * This code is free software; you can redistribute it and/or modify it
6184610Salfred * under the terms of the GNU General Public License version 2 only, as
7184610Salfred * published by the Free Software Foundation.
8184610Salfred *
9184610Salfred * This code is distributed in the hope that it will be useful, but WITHOUT
10184610Salfred * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11184610Salfred * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12184610Salfred * version 2 for more details (a copy is included in the LICENSE file that
13184610Salfred * accompanied this code).
14184610Salfred *
15184610Salfred * You should have received a copy of the GNU General Public License version
16184610Salfred * 2 along with this work; if not, write to the Free Software Foundation,
17184610Salfred * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18184610Salfred *
19184610Salfred * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20184610Salfred * or visit www.oracle.com if you need additional information or have any
21184610Salfred * questions.
22184610Salfred *
23184610Salfred */
24184610Salfred
25184610Salfred#include "precompiled.hpp"
26184610Salfred#include "code/codeCache.hpp"
27184610Salfred#include "interpreter/interpreter.hpp"
28184610Salfred#include "memory/resourceArea.hpp"
29184610Salfred#include "oops/markOop.hpp"
30184610Salfred#include "oops/method.hpp"
31184610Salfred#include "oops/oop.inline.hpp"
32184610Salfred#include "prims/methodHandles.hpp"
33184610Salfred#include "runtime/frame.inline.hpp"
34184610Salfred#include "runtime/handles.inline.hpp"
35184610Salfred#include "runtime/javaCalls.hpp"
36184610Salfred#include "runtime/monitorChunk.hpp"
37184610Salfred#include "runtime/signature.hpp"
38194677Sthompsa#include "runtime/stubCodeGenerator.hpp"
39246122Shselasky#include "runtime/stubRoutines.hpp"
40246122Shselasky#include "vmreg_sparc.inline.hpp"
41246122Shselasky#ifdef COMPILER1
42194677Sthompsa#include "c1/c1_Runtime1.hpp"
43194677Sthompsa#include "runtime/vframeArray.hpp"
44194677Sthompsa#endif
45194677Sthompsa
46194677Sthompsavoid RegisterMap::pd_clear() {
47194677Sthompsa  if (_thread->has_last_Java_frame()) {
48194677Sthompsa    frame fr = _thread->last_frame();
49194677Sthompsa    _window = fr.sp();
50194677Sthompsa  } else {
51194677Sthompsa    _window = NULL;
52194677Sthompsa  }
53194677Sthompsa  _younger_window = NULL;
54194677Sthompsa}
55194677Sthompsa
56194677Sthompsa
57194677Sthompsa// Unified register numbering scheme: each 32-bits counts as a register
58194677Sthompsa// number, so all the V9 registers take 2 slots.
59194677Sthompsaconst static int R_L_nums[] = {0+040,2+040,4+040,6+040,8+040,10+040,12+040,14+040};
60194677Sthompsaconst static int R_I_nums[] = {0+060,2+060,4+060,6+060,8+060,10+060,12+060,14+060};
61194677Sthompsaconst static int R_O_nums[] = {0+020,2+020,4+020,6+020,8+020,10+020,12+020,14+020};
62194677Sthompsaconst static int R_G_nums[] = {0+000,2+000,4+000,6+000,8+000,10+000,12+000,14+000};
63188746Sthompsastatic RegisterMap::LocationValidType bad_mask = 0;
64194677Sthompsastatic RegisterMap::LocationValidType R_LIO_mask = 0;
65184610Salfredstatic bool register_map_inited = false;
66184610Salfred
67188942Sthompsastatic void register_map_init() {
68246122Shselasky  if (!register_map_inited) {
69184610Salfred    register_map_inited = true;
70207077Sthompsa    int i;
71184610Salfred    for (i = 0; i < 8; i++) {
72184610Salfred      assert(R_L_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
73192502Sthompsa      assert(R_I_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
74192502Sthompsa      assert(R_O_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
75184610Salfred      assert(R_G_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
76184610Salfred    }
77184610Salfred
78184610Salfred    bad_mask |= (1LL << R_O_nums[6]); // SP
79184610Salfred    bad_mask |= (1LL << R_O_nums[7]); // cPC
80190181Sthompsa    bad_mask |= (1LL << R_I_nums[6]); // FP
81244503Shselasky    bad_mask |= (1LL << R_I_nums[7]); // rPC
82190181Sthompsa    bad_mask |= (1LL << R_G_nums[2]); // TLS
83184610Salfred    bad_mask |= (1LL << R_G_nums[7]); // reserved by libthread
84190733Sthompsa
85190733Sthompsa    for (i = 0; i < 8; i++) {
86190733Sthompsa      R_LIO_mask |= (1LL << R_L_nums[i]);
87190181Sthompsa      R_LIO_mask |= (1LL << R_I_nums[i]);
88190733Sthompsa      R_LIO_mask |= (1LL << R_O_nums[i]);
89190733Sthompsa    }
90190733Sthompsa  }
91190733Sthompsa}
92190173Sthompsa
93190173Sthompsa
94190173Sthompsaaddress RegisterMap::pd_location(VMReg regname) const {
95190173Sthompsa  register_map_init();
96190173Sthompsa
97190173Sthompsa  assert(regname->is_reg(), "sanity check");
98190173Sthompsa  // Only the GPRs get handled this way
99190173Sthompsa  if( !regname->is_Register())
100190173Sthompsa    return NULL;
101190173Sthompsa
102190173Sthompsa  // don't talk about bad registers
103190173Sthompsa  if ((bad_mask & ((LocationValidType)1 << regname->value())) != 0) {
104190173Sthompsa    return NULL;
105190173Sthompsa  }
106190173Sthompsa
107190173Sthompsa  // Convert to a GPR
108190173Sthompsa  Register reg;
109190173Sthompsa  int second_word = 0;
110190173Sthompsa  // 32-bit registers for in, out and local
111184610Salfred  if (!regname->is_concrete()) {
112184610Salfred    // HMM ought to return NULL for any non-concrete (odd) vmreg
113184610Salfred    // this all tied up in the fact we put out double oopMaps for
114184610Salfred    // register locations. When that is fixed we'd will return NULL
115184610Salfred    // (or assert here).
116184610Salfred    reg = regname->prev()->as_Register();
117184610Salfred#ifdef _LP64
118184610Salfred    second_word = sizeof(jint);
119184610Salfred#else
120184610Salfred    return NULL;
121184610Salfred#endif // _LP64
122184610Salfred  } else {
123184610Salfred    reg = regname->as_Register();
124184610Salfred  }
125184610Salfred  if (reg->is_out()) {
126184610Salfred    assert(_younger_window != NULL, "Younger window should be available");
127184610Salfred    return second_word + (address)&_younger_window[reg->after_save()->sp_offset_in_saved_window()];
128184610Salfred  }
129184610Salfred  if (reg->is_local() || reg->is_in()) {
130184610Salfred    assert(_window != NULL, "Window should be available");
131184610Salfred    return second_word + (address)&_window[reg->sp_offset_in_saved_window()];
132184610Salfred  }
133184610Salfred  // Only the window'd GPRs get handled this way; not the globals.
134184610Salfred  return NULL;
135184610Salfred}
136184610Salfred
137184610Salfred
138184610Salfred#ifdef ASSERT
139184610Salfredvoid RegisterMap::check_location_valid() {
140184610Salfred  register_map_init();
141184610Salfred  assert((_location_valid[0] & bad_mask) == 0, "cannot have special locations for SP,FP,TLS,etc.");
142184610Salfred}
143184610Salfred#endif
144184610Salfred
145184610Salfred// We are shifting windows.  That means we are moving all %i to %o,
146184610Salfred// getting rid of all current %l, and keeping all %g.  This is only
147184610Salfred// complicated if any of the location pointers for these are valid.
148184610Salfred// The normal case is that everything is in its standard register window
149184610Salfred// home, and _location_valid[0] is zero.  In that case, this routine
150184610Salfred// does exactly nothing.
151184610Salfredvoid RegisterMap::shift_individual_registers() {
152184610Salfred  if (!update_map())  return;  // this only applies to maps with locations
153184610Salfred  register_map_init();
154184610Salfred  check_location_valid();
155184610Salfred
156184610Salfred  LocationValidType lv = _location_valid[0];
157184610Salfred  LocationValidType lv0 = lv;
158184610Salfred
159184610Salfred  lv &= ~R_LIO_mask;  // clear %l, %o, %i regs
160184610Salfred
161184610Salfred  // if we cleared some non-%g locations, we may have to do some shifting
162184610Salfred  if (lv != lv0) {
163184610Salfred    // copy %i0-%i5 to %o0-%o5, if they have special locations
164184610Salfred    // This can happen in within stubs which spill argument registers
165184610Salfred    // around a dynamic link operation, such as resolve_opt_virtual_call.
166190181Sthompsa    for (int i = 0; i < 8; i++) {
167184610Salfred      if (lv0 & (1LL << R_I_nums[i])) {
168184610Salfred        _location[R_O_nums[i]] = _location[R_I_nums[i]];
169184610Salfred        lv |=  (1LL << R_O_nums[i]);
170184610Salfred      }
171184610Salfred    }
172184610Salfred  }
173184610Salfred
174184610Salfred  _location_valid[0] = lv;
175184610Salfred  check_location_valid();
176184610Salfred}
177184610Salfred
178184610Salfredbool frame::safe_for_sender(JavaThread *thread) {
179184610Salfred
180184610Salfred  address _SP = (address) sp();
181244503Shselasky  address _FP = (address) fp();
182244503Shselasky  address _UNEXTENDED_SP = (address) unextended_sp();
183244503Shselasky  // sp must be within the stack
184184610Salfred  bool sp_safe = (_SP <= thread->stack_base()) &&
185184610Salfred                 (_SP >= thread->stack_base() - thread->stack_size());
186184610Salfred
187184610Salfred  if (!sp_safe) {
188184610Salfred    return false;
189184610Salfred  }
190184610Salfred
191184610Salfred  // unextended sp must be within the stack and above or equal sp
192184610Salfred  bool unextended_sp_safe = (_UNEXTENDED_SP <= thread->stack_base()) &&
193184610Salfred                            (_UNEXTENDED_SP >= _SP);
194184610Salfred
195184610Salfred  if (!unextended_sp_safe) return false;
196184610Salfred
197184610Salfred  // an fp must be within the stack and above (but not equal) sp
198184610Salfred  bool fp_safe = (_FP <= thread->stack_base()) &&
199184610Salfred                 (_FP > _SP);
200184610Salfred
201184610Salfred  // We know sp/unextended_sp are safe only fp is questionable here
202184610Salfred
203184610Salfred  // If the current frame is known to the code cache then we can attempt to
204184610Salfred  // to construct the sender and do some validation of it. This goes a long way
205184610Salfred  // toward eliminating issues when we get in frame construction code
206192984Sthompsa
207192984Sthompsa  if (_cb != NULL ) {
208184610Salfred
209184610Salfred    // First check if frame is complete and tester is reliable
210184610Salfred    // Unfortunately we can only check frame complete for runtime stubs and nmethod
211184610Salfred    // other generic buffer blobs are more problematic so we just assume they are
212190733Sthompsa    // ok. adapter blobs never have a frame complete and are never ok.
213184610Salfred
214184610Salfred    if (!_cb->is_frame_complete_at(_pc)) {
215184610Salfred      if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
216184610Salfred        return false;
217184610Salfred      }
218184610Salfred    }
219184610Salfred
220184610Salfred    // Could just be some random pointer within the codeBlob
221184610Salfred    if (!_cb->code_contains(_pc)) {
222188942Sthompsa      return false;
223184610Salfred    }
224193045Sthompsa
225193045Sthompsa    // Entry frame checks
226193045Sthompsa    if (is_entry_frame()) {
227193045Sthompsa      // an entry frame must have a valid fp.
228193045Sthompsa
229184610Salfred      if (!fp_safe) {
230184610Salfred        return false;
231184610Salfred      }
232184610Salfred
233184610Salfred      // Validate the JavaCallWrapper an entry frame must have
234184610Salfred
235184610Salfred      address jcw = (address)entry_frame_call_wrapper();
236184610Salfred
237184610Salfred      bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > _FP);
238184610Salfred
239184610Salfred      return jcw_safe;
240184610Salfred
241184610Salfred    }
242184610Salfred
243184610Salfred    intptr_t* younger_sp = sp();
244184610Salfred    intptr_t* _SENDER_SP = sender_sp(); // sender is actually just _FP
245184610Salfred    bool adjusted_stack = is_interpreted_frame();
246184610Salfred
247184610Salfred    address   sender_pc = (address)younger_sp[I7->sp_offset_in_saved_window()] + pc_return_offset;
248184610Salfred
249184610Salfred
250188942Sthompsa    // We must always be able to find a recognizable pc
251184610Salfred    CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
252184610Salfred    if (sender_pc == NULL ||  sender_blob == NULL) {
253184610Salfred      return false;
254184610Salfred    }
255184610Salfred
256184610Salfred    // Could be a zombie method
257184610Salfred    if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
258184610Salfred      return false;
259246128Ssbz    }
260184610Salfred
261184610Salfred    // It should be safe to construct the sender though it might not be valid
262184610Salfred
263184610Salfred    frame sender(_SENDER_SP, younger_sp, adjusted_stack);
264184610Salfred
265184610Salfred    // Do we have a valid fp?
266184610Salfred    address sender_fp = (address) sender.fp();
267184610Salfred
268184610Salfred    // an fp must be within the stack and above (but not equal) current frame's _FP
269184610Salfred
270189275Sthompsa    bool sender_fp_safe = (sender_fp <= thread->stack_base()) &&
271184610Salfred                   (sender_fp > _FP);
272188942Sthompsa
273184610Salfred    if (!sender_fp_safe) {
274194099Sthompsa      return false;
275184610Salfred    }
276184610Salfred
277184610Salfred
278184610Salfred    // If the potential sender is the interpreter then we can do some more checking
279184610Salfred    if (Interpreter::contains(sender_pc)) {
280190733Sthompsa      return sender.is_interpreted_frame_valid(thread);
281190733Sthompsa    }
282190733Sthompsa
283184610Salfred    // Could just be some random pointer within the codeBlob
284184610Salfred    if (!sender.cb()->code_contains(sender_pc)) {
285184610Salfred      return false;
286184610Salfred    }
287184610Salfred
288184610Salfred    // We should never be able to see an adapter if the current frame is something from code cache
289190733Sthompsa    if (sender_blob->is_adapter_blob()) {
290190733Sthompsa      return false;
291190733Sthompsa    }
292190733Sthompsa
293184610Salfred    if( sender.is_entry_frame()) {
294184610Salfred      // Validate the JavaCallWrapper an entry frame must have
295184610Salfred
296184610Salfred      address jcw = (address)sender.entry_frame_call_wrapper();
297184610Salfred
298184610Salfred      bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > sender_fp);
299190733Sthompsa
300244503Shselasky      return jcw_safe;
301190733Sthompsa    }
302190733Sthompsa
303184610Salfred    // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
304184610Salfred    // because you must allocate window space
305184610Salfred
306184610Salfred    if (sender_blob->frame_size() <= 0) {
307184610Salfred      assert(!sender_blob->is_nmethod(), "should count return address at least");
308184610Salfred      return false;
309190733Sthompsa    }
310190733Sthompsa
311190733Sthompsa    // The sender should positively be an nmethod or call_stub. On sparc we might in fact see something else.
312190733Sthompsa    // The cause of this is because at a save instruction the O7 we get is a leftover from an earlier
313184610Salfred    // window use. So if a runtime stub creates two frames (common in fastdebug/debug) then we see the
314184610Salfred    // stale pc. So if the sender blob is not something we'd expect we have little choice but to declare
315184610Salfred    // the stack unwalkable. pd_get_top_frame_for_signal_handler tries to recover from this by unwinding
316184610Salfred    // that initial frame and retrying.
317184610Salfred
318184610Salfred    if (!sender_blob->is_nmethod()) {
319190733Sthompsa      return false;
320244503Shselasky    }
321190733Sthompsa
322190733Sthompsa    // Could put some more validation for the potential non-interpreted sender
323184610Salfred    // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
324184610Salfred
325184610Salfred    // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
326184610Salfred
327184610Salfred    // We've validated the potential sender that would be created
328184610Salfred
329184610Salfred    return true;
330184610Salfred
331184610Salfred  }
332184610Salfred
333192984Sthompsa  // Must be native-compiled frame. Since sender will try and use fp to find
334192984Sthompsa  // linkages it must be safe
335184610Salfred
336192499Sthompsa  if (!fp_safe) return false;
337184610Salfred
338184610Salfred  // could try and do some more potential verification of native frame if we could think of some...
339184610Salfred
340194228Sthompsa  return true;
341184610Salfred}
342184610Salfred
343184610Salfred// constructors
344184610Salfred
345184610Salfred// Construct an unpatchable, deficient frame
346184610Salfredframe::frame(intptr_t* sp, unpatchable_t, address pc, CodeBlob* cb) {
347222051Savg#ifdef _LP64
348184610Salfred  assert( (((intptr_t)sp & (wordSize-1)) == 0), "frame constructor passed an invalid sp");
349184610Salfred#endif
350184610Salfred  _sp = sp;
351184610Salfred  _younger_sp = NULL;
352184610Salfred  _pc = pc;
353184610Salfred  _cb = cb;
354192984Sthompsa  _sp_adjustment_by_callee = 0;
355192984Sthompsa  assert(pc == NULL && cb == NULL || pc != NULL, "can't have a cb and no pc!");
356184610Salfred  if (_cb == NULL && _pc != NULL ) {
357190733Sthompsa    _cb = CodeCache::find_blob(_pc);
358184610Salfred  }
359184610Salfred  _deopt_state = unknown;
360227461Shselasky#ifdef ASSERT
361184610Salfred  if ( _cb != NULL && _cb->is_nmethod()) {
362184610Salfred    // Without a valid unextended_sp() we can't convert the pc to "original"
363184610Salfred    assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant broken");
364184610Salfred  }
365184610Salfred#endif // ASSERT
366184610Salfred}
367190733Sthompsa
368184610Salfredframe::frame(intptr_t* sp, intptr_t* younger_sp, bool younger_frame_is_interpreted) :
369227462Shselasky  _sp(sp),
370227462Shselasky  _younger_sp(younger_sp),
371227462Shselasky  _deopt_state(unknown),
372190733Sthompsa  _sp_adjustment_by_callee(0) {
373184610Salfred  if (younger_sp == NULL) {
374184610Salfred    // make a deficient frame which doesn't know where its PC is
375184610Salfred    _pc = NULL;
376184610Salfred    _cb = NULL;
377184610Salfred  } else {
378184610Salfred    _pc = (address)younger_sp[I7->sp_offset_in_saved_window()] + pc_return_offset;
379227461Shselasky    assert( (intptr_t*)younger_sp[FP->sp_offset_in_saved_window()] == (intptr_t*)((intptr_t)sp - STACK_BIAS), "younger_sp must be valid");
380227461Shselasky    // Any frame we ever build should always "safe" therefore we should not have to call
381227461Shselasky    // find_blob_unsafe
382184610Salfred    // In case of native stubs, the pc retrieved here might be
383184610Salfred    // wrong.  (the _last_native_pc will have the right value)
384184610Salfred    // So do not put add any asserts on the _pc here.
385184610Salfred  }
386184610Salfred
387184610Salfred  if (_pc != NULL)
388184610Salfred    _cb = CodeCache::find_blob(_pc);
389184610Salfred
390184610Salfred  // Check for MethodHandle call sites.
391194228Sthompsa  if (_cb != NULL) {
392184610Salfred    nmethod* nm = _cb->as_nmethod_or_null();
393184610Salfred    if (nm != NULL) {
394184610Salfred      if (nm->is_deopt_mh_entry(_pc) || nm->is_method_handle_return(_pc)) {
395184610Salfred        _sp_adjustment_by_callee = (intptr_t*) ((intptr_t) sp[L7_mh_SP_save->sp_offset_in_saved_window()] + STACK_BIAS) - sp;
396184610Salfred        // The SP is already adjusted by this MH call site, don't
397184610Salfred        // overwrite this value with the wrong interpreter value.
398194228Sthompsa        younger_frame_is_interpreted = false;
399184610Salfred      }
400184610Salfred    }
401184610Salfred  }
402184610Salfred
403184610Salfred  if (younger_frame_is_interpreted) {
404184610Salfred    // compute adjustment to this frame's SP made by its interpreted callee
405184610Salfred    _sp_adjustment_by_callee = (intptr_t*) ((intptr_t) younger_sp[I5_savedSP->sp_offset_in_saved_window()] + STACK_BIAS) - sp;
406194228Sthompsa  }
407184610Salfred
408184610Salfred  // It is important that the frame is fully constructed when we do
409184610Salfred  // this lookup as get_deopt_original_pc() needs a correct value for
410184610Salfred  // unextended_sp() which uses _sp_adjustment_by_callee.
411194228Sthompsa  if (_pc != NULL) {
412184610Salfred    address original_pc = nmethod::get_deopt_original_pc(this);
413184610Salfred    if (original_pc != NULL) {
414244503Shselasky      _pc = original_pc;
415244503Shselasky      _deopt_state = is_deoptimized;
416244503Shselasky    } else {
417244503Shselasky      _deopt_state = not_deoptimized;
418244503Shselasky    }
419244503Shselasky  }
420244503Shselasky}
421244503Shselasky
422184610Salfredbool frame::is_interpreted_frame() const  {
423184610Salfred  return Interpreter::contains(pc());
424184610Salfred}
425184610Salfred
426184610Salfred// sender_sp
427184610Salfred
428184610Salfredintptr_t* frame::interpreter_frame_sender_sp() const {
429184610Salfred  assert(is_interpreted_frame(), "interpreted frame expected");
430184610Salfred  return fp();
431184610Salfred}
432184610Salfred
433184610Salfred#ifndef CC_INTERP
434184610Salfredvoid frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
435184610Salfred  assert(is_interpreted_frame(), "interpreted frame expected");
436184610Salfred  Unimplemented();
437184610Salfred}
438184610Salfred#endif // CC_INTERP
439184610Salfred
440184610Salfredframe frame::sender_for_entry_frame(RegisterMap *map) const {
441184610Salfred  assert(map != NULL, "map must be set");
442194228Sthompsa  // Java frame called from C; skip all C frames and return top C
443184610Salfred  // frame of that chunk as the sender
444184610Salfred  JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
445184610Salfred  assert(!entry_frame_is_first(), "next Java fp must be non zero");
446184610Salfred  assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
447184610Salfred  intptr_t* last_Java_sp = jfa->last_Java_sp();
448184610Salfred  // Since we are walking the stack now this nested anchor is obviously walkable
449184610Salfred  // even if it wasn't when it was stacked.
450184610Salfred  if (!jfa->walkable()) {
451184610Salfred    // Capture _last_Java_pc (if needed) and mark anchor walkable.
452184610Salfred    jfa->capture_last_Java_pc(_sp);
453184610Salfred  }
454184610Salfred  assert(jfa->last_Java_pc() != NULL, "No captured pc!");
455184610Salfred  map->clear();
456184610Salfred  map->make_integer_regs_unsaved();
457184610Salfred  map->shift_window(last_Java_sp, NULL);
458184610Salfred  assert(map->include_argument_oops(), "should be set by clear");
459184610Salfred  return frame(last_Java_sp, frame::unpatchable, jfa->last_Java_pc());
460184610Salfred}
461184610Salfred
462184610Salfredframe frame::sender_for_interpreter_frame(RegisterMap *map) const {
463184610Salfred  ShouldNotCallThis();
464184610Salfred  return sender(map);
465184610Salfred}
466184610Salfred
467184610Salfredframe frame::sender_for_compiled_frame(RegisterMap *map) const {
468184610Salfred  ShouldNotCallThis();
469184610Salfred  return sender(map);
470184610Salfred}
471184610Salfred
472194228Sthompsaframe frame::sender(RegisterMap* map) const {
473184610Salfred  assert(map != NULL, "map must be set");
474184610Salfred
475184610Salfred  assert(CodeCache::find_blob_unsafe(_pc) == _cb, "inconsistent");
476184610Salfred
477184610Salfred  // Default is not to follow arguments; update it accordingly below
478184610Salfred  map->set_include_argument_oops(false);
479194228Sthompsa
480184610Salfred  if (is_entry_frame()) return sender_for_entry_frame(map);
481194228Sthompsa
482184610Salfred  intptr_t* younger_sp = sp();
483184610Salfred  intptr_t* sp         = sender_sp();
484184610Salfred
485184610Salfred  // Note:  The version of this operation on any platform with callee-save
486184610Salfred  //        registers must update the register map (if not null).
487184610Salfred  //        In order to do this correctly, the various subtypes of
488195121Sthompsa  //        of frame (interpreted, compiled, glue, native),
489184610Salfred  //        must be distinguished.  There is no need on SPARC for
490184610Salfred  //        such distinctions, because all callee-save registers are
491192984Sthompsa  //        preserved for all frames via SPARC-specific mechanisms.
492195121Sthompsa  //
493184610Salfred  //        *** HOWEVER, *** if and when we make any floating-point
494184610Salfred  //        registers callee-saved, then we will have to copy over
495192057Sthompsa  //        the RegisterMap update logic from the Intel code.
496192057Sthompsa
497184610Salfred  // The constructor of the sender must know whether this frame is interpreted so it can set the
498184610Salfred  // sender's _sp_adjustment_by_callee field.  An osr adapter frame was originally
499184610Salfred  // interpreted but its pc is in the code cache (for c1 -> osr_frame_return_id stub), so it must be
500184610Salfred  // explicitly recognized.
501184610Salfred
502184610Salfred
503184610Salfred  bool frame_is_interpreted = is_interpreted_frame();
504184610Salfred  if (frame_is_interpreted) {
505192057Sthompsa    map->make_integer_regs_unsaved();
506192057Sthompsa    map->shift_window(sp, younger_sp);
507184610Salfred  } else if (_cb != NULL) {
508184610Salfred    // Update the locations of implicitly saved registers to be their
509184610Salfred    // addresses in the register save area.
510184610Salfred    // For %o registers, the addresses of %i registers in the next younger
511184610Salfred    // frame are used.
512184610Salfred    map->shift_window(sp, younger_sp);
513184610Salfred    if (map->update_map()) {
514184610Salfred      // Tell GC to use argument oopmaps for some runtime stubs that need it.
515184610Salfred      // For C1, the runtime stub might not have oop maps, so set this flag
516184610Salfred      // outside of update_register_map.
517184610Salfred      map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
518184610Salfred      if (_cb->oop_maps() != NULL) {
519184610Salfred        OopMapSet::update_register_map(this, map);
520194677Sthompsa      }
521184610Salfred    }
522194677Sthompsa  }
523184610Salfred  return frame(sp, younger_sp, frame_is_interpreted);
524194677Sthompsa}
525184610Salfred
526184610Salfred
527184610Salfredvoid frame::patch_pc(Thread* thread, address pc) {
528184610Salfred  if(thread == Thread::current()) {
529184610Salfred   StubRoutines::Sparc::flush_callers_register_windows_func()();
530184610Salfred  }
531244503Shselasky  if (TracePcPatching) {
532184610Salfred    // QQQ this assert is invalid (or too strong anyway) sice _pc could
533184610Salfred    // be original pc and frame could have the deopt pc.
534184610Salfred    // assert(_pc == *O7_addr() + pc_return_offset, "frame has wrong pc");
535184610Salfred    tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
536184610Salfred                  p2i(O7_addr()), p2i(_pc), p2i(pc));
537184610Salfred  }
538244503Shselasky  _cb = CodeCache::find_blob(pc);
539184610Salfred  *O7_addr() = pc - pc_return_offset;
540184610Salfred  _cb = CodeCache::find_blob(_pc);
541244503Shselasky  address original_pc = nmethod::get_deopt_original_pc(this);
542184610Salfred  if (original_pc != NULL) {
543184610Salfred    assert(original_pc == _pc, "expected original to be stored before patching");
544244503Shselasky    _deopt_state = is_deoptimized;
545184610Salfred  } else {
546184610Salfred    _deopt_state = not_deoptimized;
547184610Salfred  }
548184610Salfred}
549244503Shselasky
550184610Salfred
551184610Salfredstatic bool sp_is_valid(intptr_t* old_sp, intptr_t* young_sp, intptr_t* sp) {
552184610Salfred  return (((intptr_t)sp & (2*wordSize-1)) == 0 &&
553184610Salfred          sp <= old_sp &&
554184610Salfred          sp >= young_sp);
555244503Shselasky}
556184610Salfred
557184610Salfred
558184610Salfred/*
559184610Salfred  Find the (biased) sp that is just younger than old_sp starting at sp.
560244503Shselasky  If not found return NULL. Register windows are assumed to be flushed.
561184610Salfred*/
562184610Salfredintptr_t* frame::next_younger_sp_or_null(intptr_t* old_sp, intptr_t* sp) {
563184610Salfred
564184610Salfred  intptr_t* previous_sp = NULL;
565184610Salfred  intptr_t* orig_sp = sp;
566184610Salfred
567244503Shselasky  int max_frames = (old_sp - sp) / 16; // Minimum frame size is 16
568244503Shselasky  int max_frame2 = max_frames;
569184610Salfred  while(sp != old_sp && sp_is_valid(old_sp, orig_sp, sp)) {
570184610Salfred    if (max_frames-- <= 0)
571184610Salfred      // too many frames have gone by; invalid parameters given to this function
572184610Salfred      break;
573184610Salfred    previous_sp = sp;
574184610Salfred    sp = (intptr_t*)sp[FP->sp_offset_in_saved_window()];
575184610Salfred    sp = (intptr_t*)((intptr_t)sp + STACK_BIAS);
576194677Sthompsa  }
577194677Sthompsa
578184610Salfred  return (sp == old_sp ? previous_sp : NULL);
579184610Salfred}
580184610Salfred
581184610Salfred/*
582184610Salfred  Determine if "sp" is a valid stack pointer. "sp" is assumed to be younger than
583184610Salfred  "valid_sp". So if "sp" is valid itself then it should be possible to walk frames
584184610Salfred  from "sp" to "valid_sp". The assumption is that the registers windows for the
585194677Sthompsa  thread stack in question are flushed.
586184610Salfred*/
587184610Salfredbool frame::is_valid_stack_pointer(intptr_t* valid_sp, intptr_t* sp) {
588184610Salfred  return next_younger_sp_or_null(valid_sp, sp) != NULL;
589184610Salfred}
590184610Salfred
591184610Salfred
592184610Salfredbool frame::interpreter_frame_equals_unpacked_fp(intptr_t* fp) {
593184610Salfred  assert(is_interpreted_frame(), "must be interpreter frame");
594184610Salfred  return this->fp() == fp;
595184610Salfred}
596184610Salfred
597184610Salfred
598184610Salfredvoid frame::pd_gc_epilog() {
599184610Salfred  if (is_interpreted_frame()) {
600184610Salfred    // set constant pool cache entry for interpreter
601184610Salfred    Method* m = interpreter_frame_method();
602184610Salfred
603184610Salfred    *interpreter_frame_cpoolcache_addr() = m->constants()->cache();
604184610Salfred  }
605184610Salfred}
606184610Salfred
607194677Sthompsa
608184610Salfredbool frame::is_interpreted_frame_valid(JavaThread* thread) const {
609184610Salfred#ifdef CC_INTERP
610244650Shselasky  // Is there anything to do?
611244650Shselasky#else
612194228Sthompsa  assert(is_interpreted_frame(), "Not an interpreted frame");
613184610Salfred  // These are reasonable sanity checks
614184610Salfred  if (fp() == 0 || (intptr_t(fp()) & (2*wordSize-1)) != 0) {
615184610Salfred    return false;
616184610Salfred  }
617194677Sthompsa  if (sp() == 0 || (intptr_t(sp()) & (2*wordSize-1)) != 0) {
618184610Salfred    return false;
619184610Salfred  }
620184610Salfred
621194677Sthompsa  const intptr_t interpreter_frame_initial_sp_offset = interpreter_frame_vm_local_words;
622184610Salfred  if (fp() + interpreter_frame_initial_sp_offset < sp()) {
623194677Sthompsa    return false;
624184610Salfred  }
625184610Salfred  // These are hacks to keep us out of trouble.
626184610Salfred  // The problem with these is that they mask other problems
627194677Sthompsa  if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
628244503Shselasky    return false;
629184610Salfred  }
630244503Shselasky  // do some validation of frame elements
631184610Salfred
632184610Salfred  // first the method
633184610Salfred
634184610Salfred  Method* m = *interpreter_frame_method_addr();
635184610Salfred
636184610Salfred  // validate the method we'd find in this potential sender
637184610Salfred  if (!m->is_valid_method()) return false;
638184610Salfred
639184610Salfred  // stack frames shouldn't be much larger than max_stack elements
640184610Salfred
641184610Salfred  if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
642184610Salfred    return false;
643184610Salfred  }
644184610Salfred
645184610Salfred  // validate bci/bcx
646184610Salfred
647194677Sthompsa  intptr_t  bcx    = interpreter_frame_bcx();
648184610Salfred  if (m->validate_bci_from_bcx(bcx) < 0) {
649194677Sthompsa    return false;
650194677Sthompsa  }
651194677Sthompsa
652184610Salfred  // validate ConstantPoolCache*
653194677Sthompsa  ConstantPoolCache* cp = *interpreter_frame_cache_addr();
654194677Sthompsa  if (cp == NULL || !cp->is_metaspace_object()) return false;
655184610Salfred
656184610Salfred  // validate locals
657184610Salfred
658184610Salfred  address locals =  (address) *interpreter_frame_locals_addr();
659194677Sthompsa
660194677Sthompsa  if (locals > thread->stack_base() || locals < (address) fp()) return false;
661184610Salfred
662194677Sthompsa  // We'd have to be pretty unlucky to be mislead at this point
663184610Salfred#endif /* CC_INTERP */
664184610Salfred  return true;
665184610Salfred}
666184610Salfred
667184610Salfred
668184610Salfred// Windows have been flushed on entry (but not marked). Capture the pc that
669184610Salfred// is the return address to the frame that contains "sp" as its stack pointer.
670184610Salfred// This pc resides in the called of the frame corresponding to "sp".
671184610Salfred// As a side effect we mark this JavaFrameAnchor as having flushed the windows.
672184610Salfred// This side effect lets us mark stacked JavaFrameAnchors (stacked in the
673184610Salfred// call_helper) as flushed when we have flushed the windows for the most
674184610Salfred// recent (i.e. current) JavaFrameAnchor. This saves useless flushing calls
675184610Salfred// and lets us find the pc just once rather than multiple times as it did
676184610Salfred// in the bad old _post_Java_state days.
677194677Sthompsa//
678184610Salfredvoid JavaFrameAnchor::capture_last_Java_pc(intptr_t* sp) {
679194677Sthompsa  if (last_Java_sp() != NULL && last_Java_pc() == NULL) {
680194228Sthompsa    // try and find the sp just younger than _last_Java_sp
681184610Salfred    intptr_t* _post_Java_sp = frame::next_younger_sp_or_null(last_Java_sp(), sp);
682184610Salfred    // Really this should never fail otherwise VM call must have non-standard
683184610Salfred    // frame linkage (bad) or stack is not properly flushed (worse).
684194677Sthompsa    guarantee(_post_Java_sp != NULL, "bad stack!");
685184610Salfred    _last_Java_pc = (address) _post_Java_sp[ I7->sp_offset_in_saved_window()] + frame::pc_return_offset;
686184610Salfred
687184610Salfred  }
688184610Salfred  set_window_flushed();
689184610Salfred}
690194677Sthompsa
691184610Salfredvoid JavaFrameAnchor::make_walkable(JavaThread* thread) {
692194677Sthompsa  if (walkable()) return;
693184610Salfred  // Eventually make an assert
694184610Salfred  guarantee(Thread::current() == (Thread*)thread, "only current thread can flush its registers");
695184610Salfred  // We always flush in case the profiler wants it but we won't mark
696184610Salfred  // the windows as flushed unless we have a last_Java_frame
697184610Salfred  intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
698184610Salfred  if (last_Java_sp() != NULL ) {
699194677Sthompsa    capture_last_Java_pc(sp);
700184610Salfred  }
701194677Sthompsa}
702194677Sthompsa
703194677Sthompsaintptr_t* frame::entry_frame_argument_at(int offset) const {
704184610Salfred  // convert offset to index to deal with tsi
705194677Sthompsa  int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
706194677Sthompsa
707184610Salfred  intptr_t* LSP = (intptr_t*) sp()[Lentry_args->sp_offset_in_saved_window()];
708184610Salfred  return &LSP[index+1];
709184610Salfred}
710184610Salfred
711244503Shselasky
712244503ShselaskyBasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
713244503Shselasky  assert(is_interpreted_frame(), "interpreted frame expected");
714194677Sthompsa  Method* method = interpreter_frame_method();
715194677Sthompsa  BasicType type = method->result_type();
716194677Sthompsa
717184610Salfred  if (method->is_native()) {
718194677Sthompsa    // Prior to notifying the runtime of the method_exit the possible result
719184610Salfred    // value is saved to l_scratch and d_scratch.
720184610Salfred
721184610Salfred#ifdef CC_INTERP
722184610Salfred    interpreterState istate = get_interpreterState();
723184610Salfred    intptr_t* l_scratch = (intptr_t*) &istate->_native_lresult;
724184610Salfred    intptr_t* d_scratch = (intptr_t*) &istate->_native_fresult;
725184610Salfred#else /* CC_INTERP */
726184610Salfred    intptr_t* l_scratch = fp() + interpreter_frame_l_scratch_fp_offset;
727184610Salfred    intptr_t* d_scratch = fp() + interpreter_frame_d_scratch_fp_offset;
728184610Salfred#endif /* CC_INTERP */
729184610Salfred
730184610Salfred    address l_addr = (address)l_scratch;
731184610Salfred#ifdef _LP64
732184610Salfred    // On 64-bit the result for 1/8/16/32-bit result types is in the other
733194677Sthompsa    // word half
734184610Salfred    l_addr += wordSize/2;
735184610Salfred#endif
736244503Shselasky
737194228Sthompsa    switch (type) {
738184610Salfred      case T_OBJECT:
739184610Salfred      case T_ARRAY: {
740184610Salfred#ifdef CC_INTERP
741194677Sthompsa        *oop_result = istate->_oop_temp;
742184610Salfred#else
743184610Salfred        oop obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
744184610Salfred        assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
745194677Sthompsa        *oop_result = obj;
746184610Salfred#endif // CC_INTERP
747194677Sthompsa        break;
748184610Salfred      }
749184610Salfred
750184610Salfred      case T_BOOLEAN : { jint* p = (jint*)l_addr; value_result->z = (jboolean)((*p) & 0x1); break; }
751184610Salfred      case T_BYTE    : { jint* p = (jint*)l_addr; value_result->b = (jbyte)((*p) & 0xff); break; }
752184610Salfred      case T_CHAR    : { jint* p = (jint*)l_addr; value_result->c = (jchar)((*p) & 0xffff); break; }
753184610Salfred      case T_SHORT   : { jint* p = (jint*)l_addr; value_result->s = (jshort)((*p) & 0xffff); break; }
754194677Sthompsa      case T_INT     : value_result->i = *(jint*)l_addr; break;
755184610Salfred      case T_LONG    : value_result->j = *(jlong*)l_scratch; break;
756194677Sthompsa      case T_FLOAT   : value_result->f = *(jfloat*)d_scratch; break;
757194677Sthompsa      case T_DOUBLE  : value_result->d = *(jdouble*)d_scratch; break;
758194677Sthompsa      case T_VOID    : /* Nothing to do */ break;
759184610Salfred      default        : ShouldNotReachHere();
760194677Sthompsa    }
761194677Sthompsa  } else {
762184610Salfred    intptr_t* tos_addr = interpreter_frame_tos_address();
763184610Salfred
764184610Salfred    switch(type) {
765184610Salfred      case T_OBJECT:
766194677Sthompsa      case T_ARRAY: {
767194677Sthompsa        oop obj = cast_to_oop(*tos_addr);
768194677Sthompsa        assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
769184610Salfred        *oop_result = obj;
770194677Sthompsa        break;
771184610Salfred      }
772184610Salfred      case T_BOOLEAN : { jint* p = (jint*)tos_addr; value_result->z = (jboolean)((*p) & 0x1); break; }
773184610Salfred      case T_BYTE    : { jint* p = (jint*)tos_addr; value_result->b = (jbyte)((*p) & 0xff); break; }
774184610Salfred      case T_CHAR    : { jint* p = (jint*)tos_addr; value_result->c = (jchar)((*p) & 0xffff); break; }
775184610Salfred      case T_SHORT   : { jint* p = (jint*)tos_addr; value_result->s = (jshort)((*p) & 0xffff); break; }
776184610Salfred      case T_INT     : value_result->i = *(jint*)tos_addr; break;
777184610Salfred      case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
778184610Salfred      case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
779184610Salfred      case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
780194677Sthompsa      case T_VOID    : /* Nothing to do */ break;
781194677Sthompsa      default        : ShouldNotReachHere();
782194677Sthompsa    }
783194677Sthompsa  };
784194677Sthompsa
785194677Sthompsa  return type;
786184610Salfred}
787184610Salfred
788184610Salfred// Lesp pointer is one word lower than the top item on the stack.
789194677Sthompsaintptr_t* frame::interpreter_frame_tos_at(jint offset) const {
790184610Salfred  int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize) - 1;
791184610Salfred  return &interpreter_frame_tos_address()[index];
792244503Shselasky}
793244503Shselasky
794244503Shselasky
795244503Shselasky#ifndef PRODUCT
796194228Sthompsa
797184610Salfred#define DESCRIBE_FP_OFFSET(name) \
798184610Salfred  values.describe(frame_no, fp() + frame::name##_offset, #name)
799184610Salfred
800194677Sthompsavoid frame::describe_pd(FrameValues& values, int frame_no) {
801184610Salfred  for (int w = 0; w < frame::register_save_words; w++) {
802184610Salfred    values.describe(frame_no, sp() + w, err_msg("register save area word %d", w), 1);
803184610Salfred  }
804184610Salfred
805184610Salfred  if (is_interpreted_frame()) {
806184610Salfred#ifndef CC_INTERP
807194677Sthompsa    DESCRIBE_FP_OFFSET(interpreter_frame_d_scratch_fp);
808184610Salfred    DESCRIBE_FP_OFFSET(interpreter_frame_l_scratch_fp);
809194677Sthompsa    DESCRIBE_FP_OFFSET(interpreter_frame_padding);
810184610Salfred    DESCRIBE_FP_OFFSET(interpreter_frame_oop_temp);
811184610Salfred
812184610Salfred    // esp, according to Lesp (e.g. not depending on bci), if seems valid
813184610Salfred    intptr_t* esp = *interpreter_frame_esp_addr();
814184610Salfred    if ((esp >= sp()) && (esp < fp())) {
815184610Salfred      values.describe(-1, esp, "*Lesp");
816194677Sthompsa    }
817184610Salfred#endif
818194677Sthompsa  }
819184610Salfred
820184610Salfred  if (!is_compiled_frame()) {
821184610Salfred    if (frame::callee_aggregate_return_pointer_words != 0) {
822184610Salfred      values.describe(frame_no, sp() + frame::callee_aggregate_return_pointer_sp_offset, "callee_aggregate_return_pointer_word");
823184610Salfred    }
824184610Salfred    for (int w = 0; w < frame::callee_register_argument_save_area_words; w++) {
825184610Salfred      values.describe(frame_no, sp() + frame::callee_register_argument_save_area_sp_offset + w,
826184610Salfred                      err_msg("callee_register_argument_save_area_words %d", w));
827184610Salfred    }
828184610Salfred  }
829244503Shselasky}
830244503Shselasky
831184610Salfred#endif
832184610Salfred
833184610Salfredintptr_t *frame::initial_deoptimization_info() {
834194677Sthompsa  // unused... but returns fp() to minimize changes introduced by 7087445
835184610Salfred  return fp();
836244650Shselasky}
837244650Shselasky