frame_x86.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_frame_x86.cpp.incl"
27
28#ifdef ASSERT
29void RegisterMap::check_location_valid() {
30}
31#endif
32
33
34// Profiling/safepoint support
35
36bool frame::safe_for_sender(JavaThread *thread) {
37  address   sp = (address)_sp;
38  address   fp = (address)_fp;
39  address   unextended_sp = (address)_unextended_sp;
40  bool sp_safe = (sp != NULL &&
41                 (sp <= thread->stack_base()) &&
42                 (sp >= thread->stack_base() - thread->stack_size()));
43  bool unextended_sp_safe = (unextended_sp != NULL &&
44                 (unextended_sp <= thread->stack_base()) &&
45                 (unextended_sp >= thread->stack_base() - thread->stack_size()));
46  bool fp_safe = (fp != NULL &&
47                 (fp <= thread->stack_base()) &&
48                 (fp >= thread->stack_base() - thread->stack_size()));
49  if (sp_safe && unextended_sp_safe && fp_safe) {
50    // Unfortunately we can only check frame complete for runtime stubs and nmethod
51    // other generic buffer blobs are more problematic so we just assume they are
52    // ok. adapter blobs never have a frame complete and are never ok.
53    if (_cb != NULL && !_cb->is_frame_complete_at(_pc)) {
54      if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
55        return false;
56      }
57    }
58    return true;
59  }
60  // Note: fp == NULL is not really a prerequisite for this to be safe to
61  // walk for c2. However we've modified the code such that if we get
62  // a failure with fp != NULL that we then try with FP == NULL.
63  // This is basically to mimic what a last_frame would look like if
64  // c2 had generated it.
65  if (sp_safe && unextended_sp_safe && fp == NULL) {
66    // frame must be complete if fp == NULL as fp == NULL is only sensible
67    // if we are looking at a nmethod and frame complete assures us of that.
68    if (_cb != NULL && _cb->is_frame_complete_at(_pc) && _cb->is_compiled_by_c2()) {
69        return true;
70    }
71  }
72  return false;
73}
74
75
76void frame::patch_pc(Thread* thread, address pc) {
77  if (TracePcPatching) {
78    tty->print_cr("patch_pc at address  0x%x [0x%x -> 0x%x] ", &((address *)sp())[-1], ((address *)sp())[-1], pc);
79  }
80  ((address *)sp())[-1] = pc;
81  _cb = CodeCache::find_blob(pc);
82  if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
83    address orig = (((nmethod*)_cb)->get_original_pc(this));
84    assert(orig == _pc, "expected original to be stored before patching");
85    _deopt_state = is_deoptimized;
86    // leave _pc as is
87  } else {
88    _deopt_state = not_deoptimized;
89    _pc = pc;
90  }
91}
92
93bool frame::is_interpreted_frame() const  {
94  return Interpreter::contains(pc());
95}
96
97int frame::frame_size() const {
98  RegisterMap map(JavaThread::current(), false);
99  frame sender = this->sender(&map);
100  return sender.sp() - sp();
101}
102
103intptr_t* frame::entry_frame_argument_at(int offset) const {
104  // convert offset to index to deal with tsi
105  int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
106  // Entry frame's arguments are always in relation to unextended_sp()
107  return &unextended_sp()[index];
108}
109
110// sender_sp
111#ifdef CC_INTERP
112intptr_t* frame::interpreter_frame_sender_sp() const {
113  assert(is_interpreted_frame(), "interpreted frame expected");
114  // QQQ why does this specialize method exist if frame::sender_sp() does same thing?
115  // seems odd and if we always know interpreted vs. non then sender_sp() is really
116  // doing too much work.
117  return get_interpreterState()->sender_sp();
118}
119
120// monitor elements
121
122BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
123  return get_interpreterState()->monitor_base();
124}
125
126BasicObjectLock* frame::interpreter_frame_monitor_end() const {
127  return (BasicObjectLock*) get_interpreterState()->stack_base();
128}
129
130#else // CC_INTERP
131
132intptr_t* frame::interpreter_frame_sender_sp() const {
133  assert(is_interpreted_frame(), "interpreted frame expected");
134  return (intptr_t*) at(interpreter_frame_sender_sp_offset);
135}
136
137void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
138  assert(is_interpreted_frame(), "interpreted frame expected");
139  ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
140}
141
142
143// monitor elements
144
145BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
146  return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
147}
148
149BasicObjectLock* frame::interpreter_frame_monitor_end() const {
150  BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
151  // make sure the pointer points inside the frame
152  assert((intptr_t) fp() >  (intptr_t) result, "result must <  than frame pointer");
153  assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");
154  return result;
155}
156
157void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
158  *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
159}
160
161// Used by template based interpreter deoptimization
162void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
163    *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
164}
165#endif // CC_INTERP
166
167frame frame::sender_for_entry_frame(RegisterMap* map) const {
168  assert(map != NULL, "map must be set");
169  // Java frame called from C; skip all C frames and return top C
170  // frame of that chunk as the sender
171  JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
172  assert(!entry_frame_is_first(), "next Java fp must be non zero");
173  assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
174  map->clear();
175  assert(map->include_argument_oops(), "should be set by clear");
176  if (jfa->last_Java_pc() != NULL ) {
177    frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
178    return fr;
179  }
180  frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());
181  return fr;
182}
183
184frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
185  // sp is the raw sp from the sender after adapter or interpreter extension
186  intptr_t* sp = (intptr_t*) addr_at(sender_sp_offset);
187
188  // This is the sp before any possible extension (adapter/locals).
189  intptr_t* unextended_sp = interpreter_frame_sender_sp();
190
191  // The interpreter and compiler(s) always save EBP/RBP in a known
192  // location on entry. We must record where that location is
193  // so this if EBP/RBP was live on callout from c2 we can find
194  // the saved copy no matter what it called.
195
196  // Since the interpreter always saves EBP/RBP if we record where it is then
197  // we don't have to always save EBP/RBP on entry and exit to c2 compiled
198  // code, on entry will be enough.
199#ifdef COMPILER2
200  if (map->update_map()) {
201    map->set_location(rbp->as_VMReg(), (address) addr_at(link_offset));
202#ifdef AMD64
203    // this is weird "H" ought to be at a higher address however the
204    // oopMaps seems to have the "H" regs at the same address and the
205    // vanilla register.
206    // XXXX make this go away
207    if (true) {
208      map->set_location(rbp->as_VMReg()->next(), (address)addr_at(link_offset));
209    }
210#endif // AMD64
211  }
212#endif /* COMPILER2 */
213  return frame(sp, unextended_sp, link(), sender_pc());
214}
215
216
217//------------------------------sender_for_compiled_frame-----------------------
218frame frame::sender_for_compiled_frame(RegisterMap* map) const {
219  assert(map != NULL, "map must be set");
220  const bool c1_compiled = _cb->is_compiled_by_c1();
221
222  // frame owned by optimizing compiler
223  intptr_t* sender_sp = NULL;
224
225  assert(_cb->frame_size() >= 0, "must have non-zero frame size");
226  sender_sp = unextended_sp() + _cb->frame_size();
227
228  // On Intel the return_address is always the word on the stack
229  address sender_pc = (address) *(sender_sp-1);
230
231  // This is the saved value of ebp which may or may not really be an fp.
232  // it is only an fp if the sender is an interpreter frame (or c1?)
233
234  intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
235
236  if (map->update_map()) {
237    // Tell GC to use argument oopmaps for some runtime stubs that need it.
238    // For C1, the runtime stub might not have oop maps, so set this flag
239    // outside of update_register_map.
240    map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
241    if (_cb->oop_maps() != NULL) {
242      OopMapSet::update_register_map(this, map);
243    }
244    // Since the prolog does the save and restore of epb there is no oopmap
245    // for it so we must fill in its location as if there was an oopmap entry
246    // since if our caller was compiled code there could be live jvm state in it.
247    map->set_location(rbp->as_VMReg(), (address) (sender_sp - frame::sender_sp_offset));
248#ifdef AMD64
249    // this is weird "H" ought to be at a higher address however the
250    // oopMaps seems to have the "H" regs at the same address and the
251    // vanilla register.
252    // XXXX make this go away
253    if (true) {
254      map->set_location(rbp->as_VMReg()->next(), (address) (sender_sp - frame::sender_sp_offset));
255    }
256#endif // AMD64
257  }
258
259  assert(sender_sp != sp(), "must have changed");
260  return frame(sender_sp, saved_fp, sender_pc);
261}
262
263frame frame::sender(RegisterMap* map) const {
264  // Default is we done have to follow them. The sender_for_xxx will
265  // update it accordingly
266  map->set_include_argument_oops(false);
267
268  if (is_entry_frame())       return sender_for_entry_frame(map);
269  if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
270  assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
271
272  if (_cb != NULL) {
273    return sender_for_compiled_frame(map);
274  }
275  // Must be native-compiled frame, i.e. the marshaling code for native
276  // methods that exists in the core system.
277  return frame(sender_sp(), link(), sender_pc());
278}
279
280
281bool frame::interpreter_frame_equals_unpacked_fp(intptr_t* fp) {
282  assert(is_interpreted_frame(), "must be interpreter frame");
283  methodOop method = interpreter_frame_method();
284  // When unpacking an optimized frame the frame pointer is
285  // adjusted with:
286  int diff = (method->max_locals() - method->size_of_parameters()) *
287             Interpreter::stackElementWords();
288  return _fp == (fp - diff);
289}
290
291void frame::pd_gc_epilog() {
292  // nothing done here now
293}
294
295bool frame::is_interpreted_frame_valid() const {
296// QQQ
297#ifdef CC_INTERP
298#else
299  assert(is_interpreted_frame(), "Not an interpreted frame");
300  // These are reasonable sanity checks
301  if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
302    return false;
303  }
304  if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
305    return false;
306  }
307  if (fp() + interpreter_frame_initial_sp_offset < sp()) {
308    return false;
309  }
310  // These are hacks to keep us out of trouble.
311  // The problem with these is that they mask other problems
312  if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
313    return false;
314  }
315  if (fp() - sp() > 4096) {  // stack frames shouldn't be large.
316    return false;
317  }
318#endif // CC_INTERP
319  return true;
320}
321
322BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
323#ifdef CC_INTERP
324  // Needed for JVMTI. The result should always be in the interpreterState object
325  assert(false, "NYI");
326  interpreterState istate = get_interpreterState();
327#endif // CC_INTERP
328  assert(is_interpreted_frame(), "interpreted frame expected");
329  methodOop method = interpreter_frame_method();
330  BasicType type = method->result_type();
331
332  intptr_t* tos_addr;
333  if (method->is_native()) {
334    // Prior to calling into the runtime to report the method_exit the possible
335    // return value is pushed to the native stack. If the result is a jfloat/jdouble
336    // then ST0 is saved before EAX/EDX. See the note in generate_native_result
337    tos_addr = (intptr_t*)sp();
338    if (type == T_FLOAT || type == T_DOUBLE) {
339    // QQQ seems like this code is equivalent on the two platforms
340#ifdef AMD64
341      // This is times two because we do a push(ltos) after pushing XMM0
342      // and that takes two interpreter stack slots.
343      tos_addr += 2 * Interpreter::stackElementWords();
344#else
345      tos_addr += 2;
346#endif // AMD64
347    }
348  } else {
349    tos_addr = (intptr_t*)interpreter_frame_tos_address();
350  }
351
352  switch (type) {
353    case T_OBJECT  :
354    case T_ARRAY   : {
355      oop obj;
356      if (method->is_native()) {
357#ifdef CC_INTERP
358        obj = istate->_oop_temp;
359#else
360        obj = (oop) at(interpreter_frame_oop_temp_offset);
361#endif // CC_INTERP
362      } else {
363        oop* obj_p = (oop*)tos_addr;
364        obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
365      }
366      assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
367      *oop_result = obj;
368      break;
369    }
370    case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
371    case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
372    case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
373    case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
374    case T_INT     : value_result->i = *(jint*)tos_addr; break;
375    case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
376    case T_FLOAT   : {
377#ifdef AMD64
378        value_result->f = *(jfloat*)tos_addr;
379#else
380      if (method->is_native()) {
381        jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
382        value_result->f = (jfloat)d;
383      } else {
384        value_result->f = *(jfloat*)tos_addr;
385      }
386#endif // AMD64
387      break;
388    }
389    case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
390    case T_VOID    : /* Nothing to do */ break;
391    default        : ShouldNotReachHere();
392  }
393
394  return type;
395}
396
397
398intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
399  int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
400  return &interpreter_frame_tos_address()[index];
401}
402