1 /* 2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2007, 2008, 2009, 2010 Red Hat, Inc. 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 #if !defined(__APPLE__) && !defined(__NetBSD__) 27 #include <pthread.h> 28 # include <pthread_np.h> /* For pthread_attr_get_np */ 29 #endif 30 31 // no precompiled headers 32 #include "jvm.h" 33 #include "assembler_zero.inline.hpp" 34 #include "classfile/classLoader.hpp" 35 #include "classfile/systemDictionary.hpp" 36 #include "classfile/vmSymbols.hpp" 37 #include "code/icBuffer.hpp" 38 #include "code/vtableStubs.hpp" 39 #include "interpreter/interpreter.hpp" 40 #include "memory/allocation.inline.hpp" 41 #include "nativeInst_zero.hpp" 42 #include "os_share_bsd.hpp" 43 #include "prims/jniFastGetField.hpp" 44 #include "prims/jvm_misc.hpp" 45 #include "runtime/arguments.hpp" 46 #include "runtime/extendedPC.hpp" 47 #include "runtime/frame.inline.hpp" 48 #include "runtime/interfaceSupport.inline.hpp" 49 #include "runtime/java.hpp" 50 #include "runtime/javaCalls.hpp" 51 #include "runtime/mutexLocker.hpp" 52 #include "runtime/osThread.hpp" 53 #include "runtime/sharedRuntime.hpp" 54 #include "runtime/stubRoutines.hpp" 55 #include "runtime/thread.inline.hpp" 56 #include "runtime/timer.hpp" 57 #include "utilities/events.hpp" 58 #include "utilities/vmError.hpp" 59 60 // See stubGenerator_zero.cpp 61 #include <setjmp.h> 62 extern sigjmp_buf* get_jmp_buf_for_continuation(); 63 64 address os::current_stack_pointer() { 65 address dummy = (address) &dummy; 66 return dummy; 67 } 68 69 frame os::get_sender_for_C_frame(frame* fr) { 70 ShouldNotCallThis(); 71 return frame(); 72 } 73 74 frame os::current_frame() { 75 // The only thing that calls this is the stack printing code in 76 // VMError::report: 77 // - Step 110 (printing stack bounds) uses the sp in the frame 78 // to determine the amount of free space on the stack. We 79 // set the sp to a close approximation of the real value in 80 // order to allow this step to complete. 81 // - Step 120 (printing native stack) tries to walk the stack. 82 // The frame we create has a NULL pc, which is ignored as an 83 // invalid frame. 84 frame dummy = frame(); 85 dummy.set_sp((intptr_t *) current_stack_pointer()); 86 return dummy; 87 } 88 89 char* os::non_memory_address_word() { 90 // Must never look like an address returned by reserve_memory, 91 // even in its subfields (as defined by the CPU immediate fields, 92 // if the CPU splits constants across multiple instructions). 93 // This is the value for x86; works pretty well for PPC too. 94 return (char *) -1; 95 } 96 97 address os::Bsd::ucontext_get_pc(const ucontext_t* uc) { 98 ShouldNotCallThis(); 99 return NULL; 100 } 101 102 void os::Bsd::ucontext_set_pc(ucontext_t * uc, address pc) { 103 ShouldNotCallThis(); 104 } 105 106 ExtendedPC os::fetch_frame_from_context(const void* ucVoid, 107 intptr_t** ret_sp, 108 intptr_t** ret_fp) { 109 ShouldNotCallThis(); 110 return ExtendedPC(); 111 } 112 113 frame os::fetch_frame_from_context(const void* ucVoid) { 114 ShouldNotCallThis(); 115 return frame(); 116 } 117 118 extern "C" JNIEXPORT int 119 JVM_handle_bsd_signal(int sig, 120 siginfo_t* info, 121 void* ucVoid, 122 int abort_if_unrecognized) { 123 ucontext_t* uc = (ucontext_t*) ucVoid; 124 125 Thread* t = Thread::current_or_null_safe(); 126 127 SignalHandlerMark shm(t); 128 129 // handle SafeFetch faults 130 if (sig == SIGSEGV || sig == SIGBUS) { 131 sigjmp_buf* const pjb = get_jmp_buf_for_continuation(); 132 if (pjb) { 133 siglongjmp(*pjb, 1); 134 } 135 } 136 137 // Note: it's not uncommon that JNI code uses signal/sigset to 138 // install then restore certain signal handler (e.g. to temporarily 139 // block SIGPIPE, or have a SIGILL handler when detecting CPU 140 // type). When that happens, JVM_handle_bsd_signal() might be 141 // invoked with junk info/ucVoid. To avoid unnecessary crash when 142 // libjsig is not preloaded, try handle signals that do not require 143 // siginfo/ucontext first. 144 145 if (sig == SIGPIPE || sig == SIGXFSZ) { 146 // allow chained handler to go first 147 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 148 return true; 149 } else { 150 // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219 151 return true; 152 } 153 } 154 155 JavaThread* thread = NULL; 156 VMThread* vmthread = NULL; 157 if (os::Bsd::signal_handlers_are_installed) { 158 if (t != NULL ){ 159 if(t->is_Java_thread()) { 160 thread = (JavaThread*)t; 161 } 162 else if(t->is_VM_thread()){ 163 vmthread = (VMThread *)t; 164 } 165 } 166 } 167 168 if (info != NULL && thread != NULL) { 169 // Handle ALL stack overflow variations here 170 if (sig == SIGSEGV || sig == SIGBUS) { 171 address addr = (address) info->si_addr; 172 173 // check if fault address is within thread stack 174 if (thread->is_in_full_stack(addr)) { 175 // stack overflow 176 if (thread->in_stack_yellow_reserved_zone(addr)) { 177 thread->disable_stack_yellow_reserved_zone(); 178 ShouldNotCallThis(); 179 } 180 else if (thread->in_stack_red_zone(addr)) { 181 thread->disable_stack_red_zone(); 182 ShouldNotCallThis(); 183 } 184 } 185 } 186 187 /*if (thread->thread_state() == _thread_in_Java) { 188 ShouldNotCallThis(); 189 } 190 else*/ if ((thread->thread_state() == _thread_in_vm || 191 thread->thread_state() == _thread_in_native) && 192 sig == SIGBUS && thread->doing_unsafe_access()) { 193 ShouldNotCallThis(); 194 } 195 196 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC 197 // kicks in and the heap gets shrunk before the field access. 198 /*if (sig == SIGSEGV || sig == SIGBUS) { 199 address addr = JNI_FastGetField::find_slowcase_pc(pc); 200 if (addr != (address)-1) { 201 stub = addr; 202 } 203 }*/ 204 } 205 206 // signal-chaining 207 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 208 return true; 209 } 210 211 if (!abort_if_unrecognized) { 212 // caller wants another chance, so give it to him 213 return false; 214 } 215 216 #ifndef PRODUCT 217 if (sig == SIGSEGV) { 218 fatal("\n#" 219 "\n# /--------------------\\" 220 "\n# | segmentation fault |" 221 "\n# \\---\\ /--------------/" 222 "\n# /" 223 "\n# [-] |\\_/| " 224 "\n# (+)=C |o o|__ " 225 "\n# | | =-*-=__\\ " 226 "\n# OOO c_c_(___)"); 227 } 228 #endif // !PRODUCT 229 230 const char *fmt = 231 "caught unhandled signal " INT32_FORMAT " at address " PTR_FORMAT; 232 char buf[128]; 233 234 sprintf(buf, fmt, sig, info->si_addr); 235 fatal(buf); 236 return false; 237 } 238 239 void os::Bsd::init_thread_fpu_state(void) { 240 // Nothing to do 241 } 242 243 bool os::is_allocatable(size_t bytes) { 244 #ifdef _LP64 245 return true; 246 #else 247 if (bytes < 2 * G) { 248 return true; 249 } 250 251 char* addr = reserve_memory(bytes, NULL); 252 253 if (addr != NULL) { 254 release_memory(addr, bytes); 255 } 256 257 return addr != NULL; 258 #endif // _LP64 259 } 260 261 /////////////////////////////////////////////////////////////////////////////// 262 // thread stack 263 264 size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K; 265 size_t os::Posix::_java_thread_min_stack_allowed = 64 * K; 266 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K; 267 268 size_t os::Posix::default_stack_size(os::ThreadType thr_type) { 269 #ifdef _LP64 270 size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M); 271 #else 272 size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K); 273 #endif // _LP64 274 return s; 275 } 276 277 static void current_stack_region(address *bottom, size_t *size) { 278 address stack_bottom; 279 address stack_top; 280 size_t stack_bytes; 281 282 #ifdef __APPLE__ 283 pthread_t self = pthread_self(); 284 stack_top = (address) pthread_get_stackaddr_np(self); 285 stack_bytes = pthread_get_stacksize_np(self); 286 stack_bottom = stack_top - stack_bytes; 287 #elif defined(__OpenBSD__) 288 stack_t ss; 289 int rslt = pthread_stackseg_np(pthread_self(), &ss); 290 291 if (rslt != 0) 292 fatal("pthread_stackseg_np failed with error = " INT32_FORMAT, rslt); 293 294 stack_top = (address) ss.ss_sp; 295 stack_bytes = ss.ss_size; 296 stack_bottom = stack_top - stack_bytes; 297 #else 298 pthread_attr_t attr; 299 300 int rslt = pthread_attr_init(&attr); 301 302 // JVM needs to know exact stack location, abort if it fails 303 if (rslt != 0) 304 fatal("pthread_attr_init failed with error = " INT32_FORMAT, rslt); 305 306 rslt = pthread_attr_get_np(pthread_self(), &attr); 307 308 if (rslt != 0) 309 fatal("pthread_attr_get_np failed with error = " INT32_FORMAT, rslt); 310 311 if (pthread_attr_getstackaddr(&attr, (void **) &stack_bottom) != 0 || 312 pthread_attr_getstacksize(&attr, &stack_bytes) != 0) { 313 fatal("Can not locate current stack attributes!"); 314 } 315 316 pthread_attr_destroy(&attr); 317 318 stack_top = stack_bottom + stack_bytes; 319 #endif 320 321 assert(os::current_stack_pointer() >= stack_bottom, "should do"); 322 assert(os::current_stack_pointer() < stack_top, "should do"); 323 324 *bottom = stack_bottom; 325 *size = stack_top - stack_bottom; 326 } 327 328 address os::current_stack_base() { 329 address bottom; 330 size_t size; 331 current_stack_region(&bottom, &size); 332 return bottom + size; 333 } 334 335 size_t os::current_stack_size() { 336 // stack size includes normal stack and HotSpot guard pages 337 address bottom; 338 size_t size; 339 current_stack_region(&bottom, &size); 340 return size; 341 } 342 343 ///////////////////////////////////////////////////////////////////////////// 344 // helper functions for fatal error handler 345 346 void os::print_context(outputStream* st, const void* context) { 347 ShouldNotCallThis(); 348 } 349 350 void os::print_register_info(outputStream *st, const void *context) { 351 ShouldNotCallThis(); 352 } 353 354 ///////////////////////////////////////////////////////////////////////////// 355 // Stubs for things that would be in bsd_zero.s if it existed. 356 // You probably want to disassemble these monkeys to check they're ok. 357 358 extern "C" { 359 int SpinPause() { 360 return 1; 361 } 362 363 void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) { 364 if (from > to) { 365 const jshort *end = from + count; 366 while (from < end) 367 *(to++) = *(from++); 368 } 369 else if (from < to) { 370 const jshort *end = from; 371 from += count - 1; 372 to += count - 1; 373 while (from >= end) 374 *(to--) = *(from--); 375 } 376 } 377 void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) { 378 if (from > to) { 379 const jint *end = from + count; 380 while (from < end) 381 *(to++) = *(from++); 382 } 383 else if (from < to) { 384 const jint *end = from; 385 from += count - 1; 386 to += count - 1; 387 while (from >= end) 388 *(to--) = *(from--); 389 } 390 } 391 void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) { 392 if (from > to) { 393 const jlong *end = from + count; 394 while (from < end) 395 os::atomic_copy64(from++, to++); 396 } 397 else if (from < to) { 398 const jlong *end = from; 399 from += count - 1; 400 to += count - 1; 401 while (from >= end) 402 os::atomic_copy64(from--, to--); 403 } 404 } 405 406 void _Copy_arrayof_conjoint_bytes(const HeapWord* from, 407 HeapWord* to, 408 size_t count) { 409 memmove(to, from, count); 410 } 411 void _Copy_arrayof_conjoint_jshorts(const HeapWord* from, 412 HeapWord* to, 413 size_t count) { 414 memmove(to, from, count * 2); 415 } 416 void _Copy_arrayof_conjoint_jints(const HeapWord* from, 417 HeapWord* to, 418 size_t count) { 419 memmove(to, from, count * 4); 420 } 421 void _Copy_arrayof_conjoint_jlongs(const HeapWord* from, 422 HeapWord* to, 423 size_t count) { 424 memmove(to, from, count * 8); 425 } 426 }; 427 428 ///////////////////////////////////////////////////////////////////////////// 429 // Implementations of atomic operations not supported by processors. 430 // -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html 431 432 #ifndef _LP64 433 extern "C" { 434 long long unsigned int __sync_val_compare_and_swap_8( 435 volatile void *ptr, 436 long long unsigned int oldval, 437 long long unsigned int newval) { 438 ShouldNotCallThis(); 439 } 440 }; 441 #endif // !_LP64 442 443 #ifndef PRODUCT 444 void os::verify_stack_alignment() { 445 } 446 #endif 447 448 int os::extra_bang_size_in_bytes() { 449 // Zero does not require an additional stack bang. 450 return 0; 451 }