jdk
1/*
2* Copyright (c) 1997, 2023, 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#include "precompiled.hpp"26#include "asm/codeBuffer.hpp"27#include "asm/macroAssembler.hpp"28#include "asm/macroAssembler.inline.hpp"29#include "gc/shared/collectedHeap.hpp"30#include "memory/universe.hpp"31#include "oops/compressedOops.hpp"32#include "runtime/icache.hpp"33#include "runtime/javaThread.hpp"34#include "runtime/os.hpp"35#include "utilities/checkedCast.hpp"36
37// Implementation of AbstractAssembler
38//
39// The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster,
40// the assembler keeps a copy of the code buffers boundaries & modifies them when
41// emitting bytes rather than using the code buffers accessor functions all the time.
42// The code buffer is updated via set_code_end(...) after emitting a whole instruction.
43
44AbstractAssembler::AbstractAssembler(CodeBuffer* code) {45if (code == nullptr) return;46CodeSection* cs = code->insts();47cs->clear_mark(); // new assembler kills old mark48if (cs->start() == nullptr) {49vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "CodeCache: no room for %s", code->name());50}51_code_section = cs;52_oop_recorder= code->oop_recorder();53DEBUG_ONLY( _short_branch_delta = 0; )54}
55
56void AbstractAssembler::set_code_section(CodeSection* cs) {57assert(cs->outer() == code_section()->outer(), "sanity");58assert(cs->is_allocated(), "need to pre-allocate this section");59cs->clear_mark(); // new assembly into this section kills old mark60_code_section = cs;61}
62
63// Inform CodeBuffer that incoming code and relocation will be for stubs
64address AbstractAssembler::start_a_stub(int required_space) {65CodeBuffer* cb = code();66CodeSection* cs = cb->stubs();67assert(_code_section == cb->insts(), "not in insts?");68if (cs->maybe_expand_to_ensure_remaining(required_space)69&& cb->blob() == nullptr) {70return nullptr;71}72set_code_section(cs);73return pc();74}
75
76// Inform CodeBuffer that incoming code and relocation will be code
77// Should not be called if start_a_stub() returned null
78void AbstractAssembler::end_a_stub() {79assert(_code_section == code()->stubs(), "not in stubs?");80set_code_section(code()->insts());81}
82
83// Inform CodeBuffer that incoming code and relocation will be for stubs
84address AbstractAssembler::start_a_const(int required_space, int required_align) {85CodeBuffer* cb = code();86CodeSection* cs = cb->consts();87assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?");88address end = cs->end();89int pad = checked_cast<int>(-(intptr_t)end & (required_align-1));90if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) {91if (cb->blob() == nullptr) return nullptr;92end = cs->end(); // refresh pointer93}94if (pad > 0) {95while (--pad >= 0) { *end++ = 0; }96cs->set_end(end);97}98set_code_section(cs);99return end;100}
101
102// Inform CodeBuffer that incoming code and relocation will be code
103// in section cs (insts or stubs).
104void AbstractAssembler::end_a_const(CodeSection* cs) {105assert(_code_section == code()->consts(), "not in consts?");106set_code_section(cs);107}
108
109void AbstractAssembler::flush() {110ICache::invalidate_range(addr_at(0), offset());111}
112
113void AbstractAssembler::bind(Label& L) {114if (L.is_bound()) {115// Assembler can bind a label more than once to the same place.116guarantee(L.loc() == locator(), "attempt to redefine label");117return;118}119L.bind_loc(locator());120L.patch_instructions((MacroAssembler*)this);121}
122
123void AbstractAssembler::generate_stack_overflow_check(int frame_size_in_bytes) {124// Each code entry causes one stack bang n pages down the stack where n125// is configurable by StackShadowPages. The setting depends on the maximum126// depth of VM call stack or native before going back into java code,127// since only java code can raise a stack overflow exception using the128// stack banging mechanism. The VM and native code does not detect stack129// overflow.130// The code in JavaCalls::call() checks that there is at least n pages131// available, so all entry code needs to do is bang once for the end of132// this shadow zone.133// The entry code may need to bang additional pages if the framesize134// is greater than a page.135
136const int page_size = (int)os::vm_page_size();137int bang_end = (int)StackOverflow::stack_shadow_zone_size();138
139// This is how far the previous frame's stack banging extended.140const int bang_end_safe = bang_end;141
142if (frame_size_in_bytes > page_size) {143bang_end += frame_size_in_bytes;144}145
146int bang_offset = bang_end_safe;147while (bang_offset <= bang_end) {148// Need at least one stack bang at end of shadow zone.149bang_stack_with_offset(bang_offset);150bang_offset += page_size;151}152}
153
154void Label::add_patch_at(CodeBuffer* cb, int branch_loc, const char* file, int line) {155assert(_loc == -1, "Label is unbound");156// Don't add patch locations during scratch emit.157if (cb->insts()->scratch_emit()) { return; }158if (_patch_index < PatchCacheSize) {159_patches[_patch_index] = branch_loc;160#ifdef ASSERT161_lines[_patch_index] = line;162_files[_patch_index] = file;163#endif164} else {165if (_patch_overflow == nullptr) {166_patch_overflow = cb->create_patch_overflow();167}168_patch_overflow->push(branch_loc);169}170++_patch_index;171}
172
173void Label::patch_instructions(MacroAssembler* masm) {174assert(is_bound(), "Label is bound");175CodeBuffer* cb = masm->code();176int target_sect = CodeBuffer::locator_sect(loc());177address target = cb->locator_address(loc());178while (_patch_index > 0) {179--_patch_index;180int branch_loc;181int line = 0;182const char* file = nullptr;183if (_patch_index >= PatchCacheSize) {184branch_loc = _patch_overflow->pop();185} else {186branch_loc = _patches[_patch_index];187#ifdef ASSERT188line = _lines[_patch_index];189file = _files[_patch_index];190#endif191}192int branch_sect = CodeBuffer::locator_sect(branch_loc);193address branch = cb->locator_address(branch_loc);194if (branch_sect == CodeBuffer::SECT_CONSTS) {195// The thing to patch is a constant word.196*(address*)branch = target;197continue;198}199
200// Push the target offset into the branch instruction.201masm->pd_patch_instruction(branch, target, file, line);202}203}
204
205void AbstractAssembler::block_comment(const char* comment) {206if (sect() == CodeBuffer::SECT_INSTS) {207code_section()->outer()->block_comment(offset(), comment);208}209}
210
211const char* AbstractAssembler::code_string(const char* str) {212if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) {213return code_section()->outer()->code_string(str);214}215return nullptr;216}
217
218bool MacroAssembler::uses_implicit_null_check(void* address) {219// Exception handler checks the nmethod's implicit null checks table220// only when this method returns false.221uintptr_t addr = reinterpret_cast<uintptr_t>(address);222uintptr_t page_size = (uintptr_t)os::vm_page_size();223#ifdef _LP64224if (UseCompressedOops && CompressedOops::base() != nullptr) {225// A SEGV can legitimately happen in C2 code at address226// (heap_base + offset) if Matcher::narrow_oop_use_complex_address227// is configured to allow narrow oops field loads to be implicitly228// null checked229uintptr_t start = (uintptr_t)CompressedOops::base();230uintptr_t end = start + page_size;231if (addr >= start && addr < end) {232return true;233}234}235#endif236return addr < page_size;237}
238
239bool MacroAssembler::needs_explicit_null_check(intptr_t offset) {240// The offset -1 is used (hardcoded) in a number of places in C1 and MacroAssembler241// to indicate an unknown offset. For example, TemplateTable::pop_and_check_object(Register r)242// calls MacroAssembler::null_check(Register reg, int offset = -1) which gets here243// with -1. Another example is GraphBuilder::access_field(...) which uses -1 as placeholder244// for offsets to be patched in later. The -1 there means the offset is not yet known245// and may lie outside of the zero-trapping page, and thus we need to ensure we're forcing246// an explicit null check for -1.247
248// Check if offset is outside of [0, os::vm_page_size()]249return offset < 0 || offset >= static_cast<intptr_t>(os::vm_page_size());250}
251