aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_CODE_STUBS_HPP aoqi@0: #define SHARE_VM_CODE_STUBS_HPP aoqi@0: aoqi@0: #include "asm/codeBuffer.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "os_linux.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "os_solaris.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "os_windows.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "os_aix.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "os_bsd.inline.hpp" aoqi@0: #endif aoqi@0: aoqi@0: // The classes in this file provide a simple framework for the aoqi@0: // management of little pieces of machine code - or stubs - aoqi@0: // created on the fly and frequently discarded. In this frame- aoqi@0: // work stubs are stored in a queue. aoqi@0: aoqi@0: aoqi@0: // Stub serves as abstract base class. A concrete stub aoqi@0: // implementation is a subclass of Stub, implementing aoqi@0: // all (non-virtual!) functions required sketched out aoqi@0: // in the Stub class. aoqi@0: // aoqi@0: // A concrete stub layout may look like this (both data aoqi@0: // and code sections could be empty as well): aoqi@0: // aoqi@0: // ________ aoqi@0: // stub -->| | <--+ aoqi@0: // | data | | aoqi@0: // |________| | aoqi@0: // code_begin -->| | | aoqi@0: // | | | aoqi@0: // | code | | size aoqi@0: // | | | aoqi@0: // |________| | aoqi@0: // code_end -->| | | aoqi@0: // | data | | aoqi@0: // |________| | aoqi@0: // <--+ aoqi@0: aoqi@0: aoqi@0: class Stub VALUE_OBJ_CLASS_SPEC { aoqi@0: public: aoqi@0: // Initialization/finalization aoqi@0: void initialize(int size, aoqi@0: CodeStrings& strings) { ShouldNotCallThis(); } // called to initialize/specify the stub's size aoqi@0: void finalize() { ShouldNotCallThis(); } // called before the stub is deallocated aoqi@0: aoqi@0: // General info/converters aoqi@0: int size() const { ShouldNotCallThis(); return 0; } // must return the size provided by initialize aoqi@0: static int code_size_to_size(int code_size) { ShouldNotCallThis(); return 0; } // computes the size given the code size aoqi@0: aoqi@0: // Code info aoqi@0: address code_begin() const { ShouldNotCallThis(); return NULL; } // points to the first byte of the code aoqi@0: address code_end() const { ShouldNotCallThis(); return NULL; } // points to the first byte after the code aoqi@0: aoqi@0: // Debugging aoqi@0: void verify() { ShouldNotCallThis(); } // verifies the Stub aoqi@0: void print() { ShouldNotCallThis(); } // prints some information about the stub aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // A stub interface defines the interface between a stub queue aoqi@0: // and the stubs it queues. In order to avoid a vtable and aoqi@0: // (and thus the extra word) in each stub, a concrete stub aoqi@0: // interface object is created and associated with a stub aoqi@0: // buffer which in turn uses the stub interface to interact aoqi@0: // with its stubs. aoqi@0: // aoqi@0: // StubInterface serves as an abstract base class. A concrete aoqi@0: // stub interface implementation is a subclass of StubInterface, aoqi@0: // forwarding its virtual function calls to non-virtual calls aoqi@0: // of the concrete stub (see also macro below). There's exactly aoqi@0: // one stub interface instance required per stub queue. aoqi@0: aoqi@0: class StubInterface: public CHeapObj { aoqi@0: public: aoqi@0: // Initialization/finalization aoqi@0: virtual void initialize(Stub* self, int size, aoqi@0: CodeStrings& strings) = 0; // called after creation (called twice if allocated via (request, commit)) aoqi@0: virtual void finalize(Stub* self) = 0; // called before deallocation aoqi@0: aoqi@0: // General info/converters aoqi@0: virtual int size(Stub* self) const = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment) aoqi@0: virtual int code_size_to_size(int code_size) const = 0; // computes the total stub size in bytes given the code size in bytes aoqi@0: aoqi@0: // Code info aoqi@0: virtual address code_begin(Stub* self) const = 0; // points to the first code byte aoqi@0: virtual address code_end(Stub* self) const = 0; // points to the first byte after the code aoqi@0: aoqi@0: // Debugging aoqi@0: virtual void verify(Stub* self) = 0; // verifies the stub aoqi@0: virtual void print(Stub* self) = 0; // prints information about the stub aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // DEF_STUB_INTERFACE is used to create a concrete stub interface aoqi@0: // class, forwarding stub interface calls to the corresponding aoqi@0: // stub calls. aoqi@0: aoqi@0: #define DEF_STUB_INTERFACE(stub) \ aoqi@0: class stub##Interface: public StubInterface { \ aoqi@0: private: \ aoqi@0: static stub* cast(Stub* self) { return (stub*)self; } \ aoqi@0: \ aoqi@0: public: \ aoqi@0: /* Initialization/finalization */ \ aoqi@0: virtual void initialize(Stub* self, int size, \ aoqi@0: CodeStrings& strings) { cast(self)->initialize(size, strings); } \ aoqi@0: virtual void finalize(Stub* self) { cast(self)->finalize(); } \ aoqi@0: \ aoqi@0: /* General info */ \ aoqi@0: virtual int size(Stub* self) const { return cast(self)->size(); } \ aoqi@0: virtual int code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \ aoqi@0: \ aoqi@0: /* Code info */ \ aoqi@0: virtual address code_begin(Stub* self) const { return cast(self)->code_begin(); } \ aoqi@0: virtual address code_end(Stub* self) const { return cast(self)->code_end(); } \ aoqi@0: \ aoqi@0: /* Debugging */ \ aoqi@0: virtual void verify(Stub* self) { cast(self)->verify(); } \ aoqi@0: virtual void print(Stub* self) { cast(self)->print(); } \ aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // A StubQueue maintains a queue of stubs. aoqi@0: // Note: All sizes (spaces) are given in bytes. aoqi@0: aoqi@0: class StubQueue: public CHeapObj { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: StubInterface* _stub_interface; // the interface prototype aoqi@0: address _stub_buffer; // where all stubs are stored aoqi@0: int _buffer_size; // the buffer size in bytes aoqi@0: int _buffer_limit; // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size) aoqi@0: int _queue_begin; // the (byte) index of the first queue entry (word-aligned) aoqi@0: int _queue_end; // the (byte) index of the first entry after the queue (word-aligned) aoqi@0: int _number_of_stubs; // the number of buffered stubs aoqi@0: Mutex* const _mutex; // the lock used for a (request, commit) transaction aoqi@0: aoqi@0: void check_index(int i) const { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); } aoqi@0: bool is_contiguous() const { return _queue_begin <= _queue_end; } aoqi@0: int index_of(Stub* s) const { int i = (address)s - _stub_buffer; check_index(i); return i; } aoqi@0: Stub* stub_at(int i) const { check_index(i); return (Stub*)(_stub_buffer + i); } aoqi@0: Stub* current_stub() const { return stub_at(_queue_end); } aoqi@0: aoqi@0: // Stub functionality accessed via interface aoqi@0: void stub_initialize(Stub* s, int size, aoqi@0: CodeStrings& strings) { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size, strings); } aoqi@0: void stub_finalize(Stub* s) { _stub_interface->finalize(s); } aoqi@0: int stub_size(Stub* s) const { return _stub_interface->size(s); } aoqi@0: bool stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); } aoqi@0: int stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); } aoqi@0: void stub_verify(Stub* s) { _stub_interface->verify(s); } aoqi@0: void stub_print(Stub* s) { _stub_interface->print(s); } aoqi@0: aoqi@0: static void register_queue(StubQueue*); aoqi@0: aoqi@0: public: aoqi@0: StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock, aoqi@0: const char* name); aoqi@0: ~StubQueue(); aoqi@0: aoqi@0: // General queue info aoqi@0: bool is_empty() const { return _queue_begin == _queue_end; } aoqi@0: int total_space() const { return _buffer_size - 1; } aoqi@0: int available_space() const { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; } aoqi@0: int used_space() const { return total_space() - available_space(); } aoqi@0: int number_of_stubs() const { return _number_of_stubs; } aoqi@0: bool contains(address pc) const { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; } aoqi@0: Stub* stub_containing(address pc) const; aoqi@0: address code_start() const { return _stub_buffer; } aoqi@0: address code_end() const { return _stub_buffer + _buffer_limit; } aoqi@0: aoqi@0: // Stub allocation (atomic transactions) aoqi@0: Stub* request_committed(int code_size); // request a stub that provides exactly code_size space for code aoqi@0: Stub* request(int requested_code_size); // request a stub with a (maximum) code space - locks the queue aoqi@0: void commit (int committed_code_size, aoqi@0: CodeStrings& strings); // commit the previously requested stub - unlocks the queue aoqi@0: aoqi@0: // Stub deallocation aoqi@0: void remove_first(); // remove the first stub in the queue aoqi@0: void remove_first(int n); // remove the first n stubs in the queue aoqi@0: void remove_all(); // remove all stubs in the queue aoqi@0: aoqi@0: // Iteration aoqi@0: static void queues_do(void f(StubQueue* s)); // call f with each StubQueue aoqi@0: void stubs_do(void f(Stub* s)); // call f with all stubs aoqi@0: Stub* first() const { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; } aoqi@0: Stub* next(Stub* s) const { int i = index_of(s) + stub_size(s); aoqi@0: if (i == _buffer_limit) i = 0; aoqi@0: return (i == _queue_end) ? NULL : stub_at(i); aoqi@0: } aoqi@0: aoqi@0: address stub_code_begin(Stub* s) const { return _stub_interface->code_begin(s); } aoqi@0: address stub_code_end(Stub* s) const { return _stub_interface->code_end(s); } aoqi@0: aoqi@0: // Debugging/printing aoqi@0: void verify(); // verifies the stub queue aoqi@0: void print(); // prints information about the stub queue aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_CODE_STUBS_HPP