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