stubs.hpp revision 3465:d2a62e0f25eb
1/*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_VM_CODE_STUBS_HPP
26#define SHARE_VM_CODE_STUBS_HPP
27
28#include "memory/allocation.hpp"
29#ifdef TARGET_OS_FAMILY_linux
30# include "os_linux.inline.hpp"
31#endif
32#ifdef TARGET_OS_FAMILY_solaris
33# include "os_solaris.inline.hpp"
34#endif
35#ifdef TARGET_OS_FAMILY_windows
36# include "os_windows.inline.hpp"
37#endif
38#ifdef TARGET_OS_FAMILY_bsd
39# include "os_bsd.inline.hpp"
40#endif
41
42// The classes in this file provide a simple framework for the
43// management of little pieces of machine code - or stubs -
44// created on the fly and frequently discarded. In this frame-
45// work stubs are stored in a queue.
46
47
48// Stub serves as abstract base class. A concrete stub
49// implementation is a subclass of Stub, implementing
50// all (non-virtual!) functions required sketched out
51// in the Stub class.
52//
53// A concrete stub layout may look like this (both data
54// and code sections could be empty as well):
55//
56//                ________
57// stub       -->|        | <--+
58//               |  data  |    |
59//               |________|    |
60// code_begin -->|        |    |
61//               |        |    |
62//               |  code  |    | size
63//               |        |    |
64//               |________|    |
65// code_end   -->|        |    |
66//               |  data  |    |
67//               |________|    |
68//                          <--+
69
70
71class Stub VALUE_OBJ_CLASS_SPEC {
72 public:
73  // Initialization/finalization
74  void    initialize(int size)                   { ShouldNotCallThis(); }                // called to initialize/specify the stub's size
75  void    finalize()                             { ShouldNotCallThis(); }                // called before the stub is deallocated
76
77  // General info/converters
78  int     size() const                           { ShouldNotCallThis(); return 0; }      // must return the size provided by initialize
79  static  int code_size_to_size(int code_size)   { ShouldNotCallThis(); return 0; }      // computes the size given the code size
80
81  // Code info
82  address code_begin() const                     { ShouldNotCallThis(); return NULL; }   // points to the first byte of    the code
83  address code_end() const                       { ShouldNotCallThis(); return NULL; }   // points to the first byte after the code
84
85  // Debugging
86  void    verify()                               { ShouldNotCallThis(); }                // verifies the Stub
87  void    print()                                { ShouldNotCallThis(); }                // prints some information about the stub
88};
89
90
91// A stub interface defines the interface between a stub queue
92// and the stubs it queues. In order to avoid a vtable and
93// (and thus the extra word) in each stub, a concrete stub
94// interface object is created and associated with a stub
95// buffer which in turn uses the stub interface to interact
96// with its stubs.
97//
98// StubInterface serves as an abstract base class. A concrete
99// stub interface implementation is a subclass of StubInterface,
100// forwarding its virtual function calls to non-virtual calls
101// of the concrete stub (see also macro below). There's exactly
102// one stub interface instance required per stub queue.
103
104class StubInterface: public CHeapObj<mtCode> {
105 public:
106  // Initialization/finalization
107  virtual void    initialize(Stub* self, int size)         = 0; // called after creation (called twice if allocated via (request, commit))
108  virtual void    finalize(Stub* self)                     = 0; // called before deallocation
109
110  // General info/converters
111  virtual int     size(Stub* self) const                   = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment)
112  virtual int     code_size_to_size(int code_size) const   = 0; // computes the total stub size in bytes given the code size in bytes
113
114  // Code info
115  virtual address code_begin(Stub* self) const             = 0; // points to the first code byte
116  virtual address code_end(Stub* self) const               = 0; // points to the first byte after the code
117
118  // Debugging
119  virtual void    verify(Stub* self)                       = 0; // verifies the stub
120  virtual void    print(Stub* self)                        = 0; // prints information about the stub
121};
122
123
124// DEF_STUB_INTERFACE is used to create a concrete stub interface
125// class, forwarding stub interface calls to the corresponding
126// stub calls.
127
128#define DEF_STUB_INTERFACE(stub)                           \
129  class stub##Interface: public StubInterface {            \
130   private:                                                \
131    static stub*    cast(Stub* self)                       { return (stub*)self; }                 \
132                                                           \
133   public:                                                 \
134    /* Initialization/finalization */                      \
135    virtual void    initialize(Stub* self, int size)       { cast(self)->initialize(size); }       \
136    virtual void    finalize(Stub* self)                   { cast(self)->finalize(); }             \
137                                                           \
138    /* General info */                                     \
139    virtual int     size(Stub* self) const                 { return cast(self)->size(); }          \
140    virtual int     code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \
141                                                           \
142    /* Code info */                                        \
143    virtual address code_begin(Stub* self) const           { return cast(self)->code_begin(); }    \
144    virtual address code_end(Stub* self) const             { return cast(self)->code_end(); }      \
145                                                           \
146    /* Debugging */                                        \
147    virtual void    verify(Stub* self)                     { cast(self)->verify(); }               \
148    virtual void    print(Stub* self)                      { cast(self)->print(); }                \
149  };
150
151
152// A StubQueue maintains a queue of stubs.
153// Note: All sizes (spaces) are given in bytes.
154
155class StubQueue: public CHeapObj<mtCode> {
156  friend class VMStructs;
157 private:
158  StubInterface* _stub_interface;                // the interface prototype
159  address        _stub_buffer;                   // where all stubs are stored
160  int            _buffer_size;                   // the buffer size in bytes
161  int            _buffer_limit;                  // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size)
162  int            _queue_begin;                   // the (byte) index of the first queue entry (word-aligned)
163  int            _queue_end;                     // the (byte) index of the first entry after the queue (word-aligned)
164  int            _number_of_stubs;               // the number of buffered stubs
165  Mutex* const   _mutex;                         // the lock used for a (request, commit) transaction
166
167  void  check_index(int i) const                 { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); }
168  bool  is_contiguous() const                    { return _queue_begin <= _queue_end; }
169  int   index_of(Stub* s) const                  { int i = (address)s - _stub_buffer; check_index(i); return i; }
170  Stub* stub_at(int i) const                     { check_index(i); return (Stub*)(_stub_buffer + i); }
171  Stub* current_stub() const                     { return stub_at(_queue_end); }
172
173  // Stub functionality accessed via interface
174  void  stub_initialize(Stub* s, int size)       { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size); }
175  void  stub_finalize(Stub* s)                   { _stub_interface->finalize(s); }
176  int   stub_size(Stub* s) const                 { return _stub_interface->size(s); }
177  bool  stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); }
178  int   stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); }
179  void  stub_verify(Stub* s)                     { _stub_interface->verify(s); }
180  void  stub_print(Stub* s)                      { _stub_interface->print(s); }
181
182  static void register_queue(StubQueue*);
183
184 public:
185  StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock,
186            const char* name);
187  ~StubQueue();
188
189  // General queue info
190  bool  is_empty() const                         { return _queue_begin == _queue_end; }
191  int   total_space() const                      { return _buffer_size - 1; }
192  int   available_space() const                  { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; }
193  int   used_space() const                       { return total_space() - available_space(); }
194  int   number_of_stubs() const                  { return _number_of_stubs; }
195  bool  contains(address pc) const               { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; }
196  Stub* stub_containing(address pc) const;
197  address code_start() const                     { return _stub_buffer; }
198  address code_end() const                       { return _stub_buffer + _buffer_limit; }
199
200  // Stub allocation (atomic transactions)
201  Stub* request_committed(int code_size);        // request a stub that provides exactly code_size space for code
202  Stub* request(int requested_code_size);        // request a stub with a (maximum) code space - locks the queue
203  void  commit (int committed_code_size);        // commit the previously requested stub - unlocks the queue
204
205  // Stub deallocation
206  void  remove_first();                          // remove the first stub in the queue
207  void  remove_first(int n);                     // remove the first n stubs in the queue
208  void  remove_all();                            // remove all stubs in the queue
209
210  // Iteration
211  static void queues_do(void f(StubQueue* s));   // call f with each StubQueue
212  void  stubs_do(void f(Stub* s));               // call f with all stubs
213  Stub* first() const                            { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; }
214  Stub* next(Stub* s) const                      { int i = index_of(s) + stub_size(s);
215                                                   if (i == _buffer_limit) i = 0;
216                                                   return (i == _queue_end) ? NULL : stub_at(i);
217                                                 }
218
219  address stub_code_begin(Stub* s) const         { return _stub_interface->code_begin(s); }
220  address stub_code_end(Stub* s) const           { return _stub_interface->code_end(s);   }
221
222  // Debugging/printing
223  void  verify();                                // verifies the stub queue
224  void  print();                                 // prints information about the stub queue
225};
226
227#endif // SHARE_VM_CODE_STUBS_HPP
228