1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "asm/assembler.inline.hpp"
  27 #include "asm/macroAssembler.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "depChecker_s390.hpp"
  31 #include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
  32 #include "gc/cms/parOopClosures.inline.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "gc/shared/cardTableBarrierSet.hpp"
  35 #include "gc/shared/genOopClosures.inline.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "utilities/align.hpp"
  41 
  42 // List of all major opcodes, as of
  43 // Principles of Operation, Eleventh Edition, March 2015
  44 bool Disassembler::valid_opcodes[] =
  45 { true,  true,  false, false, true,  true,  true,  true,  // 0x00..07
  46   false, false, true,  true,  true,  true,  true,  true,  // 0x08..0f
  47   true,  true,  true,  true,  true,  true,  true,  true,  // 0x10..17
  48   true,  true,  true,  true,  true,  true,  true,  true,  // 0x18..1f
  49   true,  true,  true,  true,  true,  true,  true,  true,  // 0x20..27
  50   true,  true,  true,  true,  true,  true,  true,  true,  // 0x28..2f
  51   true,  true,  true,  true,  true,  true,  true,  true,  // 0x30..37
  52   true,  true,  true,  true,  true,  true,  true,  true,  // 0x38..3f
  53   true,  true,  true,  true,  true,  true,  true,  true,  // 0x40..47
  54   true,  true,  true,  true,  true,  true,  true,  true,  // 0x48..4f
  55   true,  true,  false, false, true,  true,  true,  true,  // 0x50..57
  56   true,  true,  true,  true,  true,  true,  true,  true,  // 0x58..5f
  57   true,  false, false, false, false, false, false, true,  // 0x60..67
  58   true,  true,  true,  true,  true,  true,  true,  true,  // 0x68..6f
  59   true,  true,  false, false, false, false, false, false, // 0x70..77
  60   true,  true,  true,  true,  true,  true,  true,  true,  // 0x78..7f
  61   true,  false, true,  true,  true,  true,  true,  true,  // 0x80..87
  62   true,  true,  true,  true,  true,  true,  true,  true,  // 0x88..8f
  63   true,  true,  true,  true,  true,  true,  true,  true,  // 0x90..97
  64   true,  true,  true,  true,  false, false, false, false, // 0x98..9f
  65   false, false, false, false, false, true,  false, true,  // 0xa0..a7
  66   true,  true,  false, false, true,  true,  true,  true,  // 0xa8..af
  67   false, true,  true,  true,  false, false, true,  true,  // 0xb0..b7
  68   false, true,  true,  true,  false, true,  true,  true,  // 0xb8..bf
  69   true,  false, true,  false, true,  false, true,  false, // 0xc0..c7
  70   true,  false, false, false, true,  false, false, false, // 0xc8..cf
  71   true,  true,  true,  true,  true,  true,  true,  true,  // 0xd0..d7
  72   false, true,  true,  true,  true,  true,  true,  true,  // 0xd8..df
  73   false, true,  true,  true,  false, true,  false, true,  // 0xe0..e7
  74   true,  true,  true,  true,  true,  true,  true,  true,  // 0xe8..ef
  75   true,  true,  true,  true,  false, false, false, false, // 0xf0..f7
  76   true,  true,  true,  true,  true,  true,  false, false, // 0xf8..ff
  77 };
  78 // Check for valid opcodes.
  79 //
  80 // The major opcode (one byte) at the passed location is inspected.
  81 // If the opcode found is assigned, the function returns true, false otherwise.
  82 // The true indication is not reliable. It may well be that the major opcode is
  83 // assigned, but there exists a minor opcode field in the instruction which
  84 // which has unassigned values.
  85 bool Disassembler::is_valid_opcode_at(address here) {
  86   return valid_opcodes[*here];
  87 }
  88 
  89 // This method does plain instruction decoding, no frills.
  90 // It may be called before the binutils disassembler kicks in
  91 // to handle special cases the binutils disassembler does not.
  92 // Instruction address, comments, and the like have to be output by caller.
  93 address Disassembler::decode_instruction0(address here, outputStream * st, address virtual_begin) {
  94   if (is_abstract()) {
  95     // The disassembler library was not loaded (yet),
  96     // use AbstractDisassembler's decode-method.
  97     return decode_instruction_abstract(here, st, Assembler::instr_len(here), Assembler::instr_maxlen());
  98   }
  99 
 100   // Currently, "special decoding" doesn't work when decoding error files.
 101   // When decoding an instruction from a hs_err file, the given
 102   // instruction address 'start' points to the instruction's virtual address
 103   // which is not equal to the address where the instruction is located.
 104   // Therefore, we will either crash or decode garbage.
 105   if (is_decode_error_file()) {
 106     return here;
 107   }
 108 
 109   //---<  Decode some well-known "instructions"  >---
 110 
 111   address  next;
 112   uint16_t instruction_2bytes = *(uint16_t*)here;
 113 
 114   if (Assembler::is_z_nop((long)instruction_2bytes)) {
 115 #if 1
 116     st->print("nop     ");  // fill up to operand column, leads to better code comment alignment
 117     next = here + 2;
 118 #else
 119     // Compact disassembler output. Does not work the easy way.
 120     // Currently unusable, search does not terminate, risk of crash.
 121     // TODO: rework required.
 122     // Terminate search loop when reaching CodeEntryAlignment-aligned offset
 123     // or, at the latest, when reaching the next page boundary.
 124     int n_nops = 0;
 125     while(is_same_page(here, here+2*n_nops) && Assembler::is_z_nop((long)instruction_2bytes)) {
 126       n_nops++;
 127       instruction_2bytes   = *(uint16_t*)(here+2*n_nops);
 128     }
 129     if (n_nops <= 4) { // do not group few subsequent nops
 130       st->print("nop     ");  // fill up to operand column, leads to better code comment alignment
 131       next = here + 2;
 132     } else {
 133       st->print("nop     count=%d", n_nops);
 134       next = here + 2*n_nops;
 135     }
 136 #endif
 137   } else if (Assembler::is_z_sync((long)instruction_2bytes)) {
 138     // Specific names. Make use of lightweight sync.
 139     st->print("sync   ");
 140     if (Assembler::is_z_sync_full((long)instruction_2bytes) ) st->print("heavyweight");
 141     if (Assembler::is_z_sync_light((long)instruction_2bytes)) st->print("lightweight");
 142     next = here + 2;
 143   } else if (instruction_2bytes == 0x0000) {
 144 #if 1
 145     st->print("illtrap .nodata");
 146     next = here + 2;
 147 #else
 148     // Compact disassembler output. Does not work the easy way.
 149     // Currently unusable, search does not terminate, risk of crash.
 150     // TODO: rework required.
 151     // Terminate search loop when reaching CodeEntryAlignment-aligned offset
 152     // or, at the latest, when reaching the next page boundary.
 153     int n_traps = 0;
 154     while(is_same_page(here, here+2*n_nops) && (instruction_2bytes == 0x0000)) {
 155       n_traps++;
 156       instruction_2bytes   = *(uint16_t*)(here+2*n_traps);
 157     }
 158     if (n_traps <= 4) { // do not group few subsequent illtraps
 159       st->print("illtrap .nodata");
 160       next = here + 2;
 161     } else {
 162       st->print("illtrap .nodata count=%d", n_traps);
 163       next = here + 2*n_traps;
 164     }
 165 #endif
 166   } else if ((instruction_2bytes & 0xff00) == 0x0000) {
 167     st->print("illtrap .data 0x%2.2x", instruction_2bytes & 0x00ff);
 168     next = here + 2;
 169   } else {
 170      next = here;
 171   }
 172   return next;
 173 }
 174 
 175 // Count the instructions contained in the range [begin..end).
 176 // The range must exactly contain the instructions, i.e.
 177 //  - the first instruction starts @begin
 178 //  - the last instruction ends @(end-1)
 179 // The caller has to make sure that the given range is readable.
 180 // This function performs no safety checks!
 181 // Return value:
 182 //  - The number of instructions, if there was exact containment.
 183 //  - If there is no exact containment, a negative value is returned.
 184 //    Its absolute value is the number of instructions from begin to end,
 185 //    where the last instruction counted runs over the range end.
 186 //  - 0 (zero) is returned if there was a parameter error
 187 //    (inverted range, bad starting point).
 188 int Disassembler::count_instr(address begin, address end) {
 189   if (end < begin+2) return 0; // no instructions in range
 190   if (!Disassembler::is_valid_opcode_at(begin)) return 0; // bad starting point
 191 
 192   address p = begin;
 193   int     n = 0;
 194   while(p < end) {
 195     p += Assembler::instr_len(p);
 196     n++;
 197   }
 198   return (p == end) ? n : -n;
 199 }
 200 
 201 // Find preceding instruction.
 202 //
 203 // Starting at the passed location, the n-th preceding (towards lower addresses)
 204 // instruction is searched. With variable length instructions, there may be
 205 // more than one solution, or no solution at all (if the passed location
 206 // does not point to the start of an instruction or if the storage area
 207 // does not contain instructions at all).
 208 // instructions - has the passed location as n-th successor.
 209 //  - If multiple such locations exist between (here-n*instr_maxlen()) and here,
 210 //    the most distant location is selected.
 211 //  - If no such location exists, NULL is returned. The caller should then
 212 //    terminate its search and react properly.
 213 // Must be placed here in disassembler_s390.cpp. It does not compile
 214 // in the header. There the class 'Assembler' is not available.
 215 address Disassembler::find_prev_instr(address here, int n_instr) {
 216   if (!os::is_readable_pointer(here)) return NULL;    // obviously a bad location to decode
 217 
 218   // Find most distant possible starting point.
 219   // Narrow down because we don't want to SEGV while printing.
 220   address start = here - n_instr*Assembler::instr_maxlen(); // starting point can't be further away.
 221   while ((start < here) && !os::is_readable_range(start, here)) {
 222     start = align_down(start, os::min_page_size()) + os::min_page_size();
 223   }
 224   if (start >= here) {
 225     // Strange. Can only happen with here on page boundary.
 226     return NULL;
 227   }
 228 
 229   //---<  Find a starting point  >---
 230   int i_count = 0;
 231   while ((start < here) && ((i_count = count_instr(start, here)) <= 0)) start += 2;
 232   if (i_count == 0) return NULL; // There is something seriously wrong
 233 
 234   //---<  Narrow down distance (estimate was too large)  >---
 235   while(i_count-- > n_instr) {
 236     start   += Assembler::instr_len(start);
 237   }
 238   assert(n_instr >= count_instr(start, here), "just checking");
 239   return start;
 240 }
 241 
 242 
 243 // Print annotations (value of loaded constant)
 244 void Disassembler::annotate(address here, outputStream* st) {
 245   // Currently, annotation doesn't work when decoding error files.
 246   // When decoding an instruction from a hs_err file, the given
 247   // instruction address 'start' points to the instruction's virtual address
 248   // which is not equal to the address where the instruction is located.
 249   // Therefore, we will either crash or decode garbage.
 250   if (is_decode_error_file()) {
 251     return;
 252   }
 253 
 254   if (MacroAssembler::is_load_const(here)) {
 255     long      value = MacroAssembler::get_const(here);
 256     const int tsize = 8;
 257 
 258     st->fill_to(60);
 259     st->print(";const %p | %ld | %23.15e", (void *)value, value, (double)value);
 260   }
 261 }