1 /* 2 * Copyright (c) 2003, 2019, 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 #ifdef SPARC 94 // On SPARC, 0 != %hi(any real address), because there is no 95 // allocation in the first 1Kb of the virtual address space. 96 return (char *) 0; 97 #else 98 // This is the value for x86; works pretty well for PPC too. 99 return (char *) -1; 100 #endif // SPARC 101 } 102 103 address os::Bsd::ucontext_get_pc(const ucontext_t* uc) { 104 ShouldNotCallThis(); 105 return NULL; 106 } 107 108 void os::Bsd::ucontext_set_pc(ucontext_t * uc, address pc) { 109 ShouldNotCallThis(); 110 } 111 112 ExtendedPC os::fetch_frame_from_context(const void* ucVoid, 113 intptr_t** ret_sp, 114 intptr_t** ret_fp) { 115 ShouldNotCallThis(); 116 return ExtendedPC(); 117 } 118 119 frame os::fetch_frame_from_context(const void* ucVoid) { 120 ShouldNotCallThis(); 121 return frame(); 122 } 123 124 extern "C" JNIEXPORT int 125 JVM_handle_bsd_signal(int sig, 126 siginfo_t* info, 127 void* ucVoid, 128 int abort_if_unrecognized) { 129 ucontext_t* uc = (ucontext_t*) ucVoid; 130 131 Thread* t = Thread::current_or_null_safe(); 132 133 SignalHandlerMark shm(t); 134 135 // handle SafeFetch faults 136 if (sig == SIGSEGV || sig == SIGBUS) { 137 sigjmp_buf* const pjb = get_jmp_buf_for_continuation(); 138 if (pjb) { 139 siglongjmp(*pjb, 1); 140 } 141 } 142 143 // Note: it's not uncommon that JNI code uses signal/sigset to 144 // install then restore certain signal handler (e.g. to temporarily 145 // block SIGPIPE, or have a SIGILL handler when detecting CPU 146 // type). When that happens, JVM_handle_bsd_signal() might be 147 // invoked with junk info/ucVoid. To avoid unnecessary crash when 148 // libjsig is not preloaded, try handle signals that do not require 149 // siginfo/ucontext first. 150 151 if (sig == SIGPIPE || sig == SIGXFSZ) { 152 // allow chained handler to go first 153 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 154 return true; 155 } else { 156 // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219 157 return true; 158 } 159 } 160 161 JavaThread* thread = NULL; 162 VMThread* vmthread = NULL; 163 if (os::Bsd::signal_handlers_are_installed) { 164 if (t != NULL ){ 165 if(t->is_Java_thread()) { 166 thread = (JavaThread*)t; 167 } 168 else if(t->is_VM_thread()){ 169 vmthread = (VMThread *)t; 170 } 171 } 172 } 173 174 if (info != NULL && thread != NULL) { 175 // Handle ALL stack overflow variations here 176 if (sig == SIGSEGV || sig == SIGBUS) { 177 address addr = (address) info->si_addr; 178 179 // check if fault address is within thread stack 180 if (thread->on_local_stack(addr)) { 181 // stack overflow 182 if (thread->in_stack_yellow_reserved_zone(addr)) { 183 thread->disable_stack_yellow_reserved_zone(); 184 ShouldNotCallThis(); 185 } 186 else if (thread->in_stack_red_zone(addr)) { 187 thread->disable_stack_red_zone(); 188 ShouldNotCallThis(); 189 } 190 } 191 } 192 193 /*if (thread->thread_state() == _thread_in_Java) { 194 ShouldNotCallThis(); 195 } 196 else*/ if ((thread->thread_state() == _thread_in_vm || 197 thread->thread_state() == _thread_in_native) && 198 sig == SIGBUS && thread->doing_unsafe_access()) { 199 ShouldNotCallThis(); 200 } 201 202 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC 203 // kicks in and the heap gets shrunk before the field access. 204 /*if (sig == SIGSEGV || sig == SIGBUS) { 205 address addr = JNI_FastGetField::find_slowcase_pc(pc); 206 if (addr != (address)-1) { 207 stub = addr; 208 } 209 }*/ 210 } 211 212 // signal-chaining 213 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 214 return true; 215 } 216 217 if (!abort_if_unrecognized) { 218 // caller wants another chance, so give it to him 219 return false; 220 } 221 222 #ifndef PRODUCT 223 if (sig == SIGSEGV) { 224 fatal("\n#" 225 "\n# /--------------------\\" 226 "\n# | segmentation fault |" 227 "\n# \\---\\ /--------------/" 228 "\n# /" 229 "\n# [-] |\\_/| " 230 "\n# (+)=C |o o|__ " 231 "\n# | | =-*-=__\\ " 232 "\n# OOO c_c_(___)"); 233 } 234 #endif // !PRODUCT 235 236 const char *fmt = 237 "caught unhandled signal " INT32_FORMAT " at address " PTR_FORMAT; 238 char buf[128]; 239 240 sprintf(buf, fmt, sig, info->si_addr); 241 fatal(buf); 242 return false; 243 } 244 245 void os::Bsd::init_thread_fpu_state(void) { 246 // Nothing to do 247 } 248 249 bool os::is_allocatable(size_t bytes) { 250 #ifdef _LP64 251 return true; 252 #else 253 if (bytes < 2 * G) { 254 return true; 255 } 256 257 char* addr = reserve_memory(bytes, NULL); 258 259 if (addr != NULL) { 260 release_memory(addr, bytes); 261 } 262 263 return addr != NULL; 264 #endif // _LP64 265 } 266 267 /////////////////////////////////////////////////////////////////////////////// 268 // thread stack 269 270 size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K; 271 size_t os::Posix::_java_thread_min_stack_allowed = 64 * K; 272 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K; 273 274 size_t os::Posix::default_stack_size(os::ThreadType thr_type) { 275 #ifdef _LP64 276 size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M); 277 #else 278 size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K); 279 #endif // _LP64 280 return s; 281 } 282 283 static void current_stack_region(address *bottom, size_t *size) { 284 address stack_bottom; 285 address stack_top; 286 size_t stack_bytes; 287 288 #ifdef __APPLE__ 289 pthread_t self = pthread_self(); 290 stack_top = (address) pthread_get_stackaddr_np(self); 291 stack_bytes = pthread_get_stacksize_np(self); 292 stack_bottom = stack_top - stack_bytes; 293 #elif defined(__OpenBSD__) 294 stack_t ss; 295 int rslt = pthread_stackseg_np(pthread_self(), &ss); 296 297 if (rslt != 0) 298 fatal("pthread_stackseg_np failed with error = " INT32_FORMAT, rslt); 299 300 stack_top = (address) ss.ss_sp; 301 stack_bytes = ss.ss_size; 302 stack_bottom = stack_top - stack_bytes; 303 #else 304 pthread_attr_t attr; 305 306 int rslt = pthread_attr_init(&attr); 307 308 // JVM needs to know exact stack location, abort if it fails 309 if (rslt != 0) 310 fatal("pthread_attr_init failed with error = " INT32_FORMAT, rslt); 311 312 rslt = pthread_attr_get_np(pthread_self(), &attr); 313 314 if (rslt != 0) 315 fatal("pthread_attr_get_np failed with error = " INT32_FORMAT, rslt); 316 317 if (pthread_attr_getstackaddr(&attr, (void **) &stack_bottom) != 0 || 318 pthread_attr_getstacksize(&attr, &stack_bytes) != 0) { 319 fatal("Can not locate current stack attributes!"); 320 } 321 322 pthread_attr_destroy(&attr); 323 324 stack_top = stack_bottom + stack_bytes; 325 #endif 326 327 assert(os::current_stack_pointer() >= stack_bottom, "should do"); 328 assert(os::current_stack_pointer() < stack_top, "should do"); 329 330 *bottom = stack_bottom; 331 *size = stack_top - stack_bottom; 332 } 333 334 address os::current_stack_base() { 335 address bottom; 336 size_t size; 337 current_stack_region(&bottom, &size); 338 return bottom + size; 339 } 340 341 size_t os::current_stack_size() { 342 // stack size includes normal stack and HotSpot guard pages 343 address bottom; 344 size_t size; 345 current_stack_region(&bottom, &size); 346 return size; 347 } 348 349 ///////////////////////////////////////////////////////////////////////////// 350 // helper functions for fatal error handler 351 352 void os::print_context(outputStream* st, const void* context) { 353 ShouldNotCallThis(); 354 } 355 356 void os::print_register_info(outputStream *st, const void *context) { 357 ShouldNotCallThis(); 358 } 359 360 ///////////////////////////////////////////////////////////////////////////// 361 // Stubs for things that would be in bsd_zero.s if it existed. 362 // You probably want to disassemble these monkeys to check they're ok. 363 364 extern "C" { 365 int SpinPause() { 366 return 1; 367 } 368 369 void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) { 370 if (from > to) { 371 jshort *end = from + count; 372 while (from < end) 373 *(to++) = *(from++); 374 } 375 else if (from < to) { 376 jshort *end = from; 377 from += count - 1; 378 to += count - 1; 379 while (from >= end) 380 *(to--) = *(from--); 381 } 382 } 383 void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) { 384 if (from > to) { 385 jint *end = from + count; 386 while (from < end) 387 *(to++) = *(from++); 388 } 389 else if (from < to) { 390 jint *end = from; 391 from += count - 1; 392 to += count - 1; 393 while (from >= end) 394 *(to--) = *(from--); 395 } 396 } 397 void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) { 398 if (from > to) { 399 jlong *end = from + count; 400 while (from < end) 401 os::atomic_copy64(from++, to++); 402 } 403 else if (from < to) { 404 jlong *end = from; 405 from += count - 1; 406 to += count - 1; 407 while (from >= end) 408 os::atomic_copy64(from--, to--); 409 } 410 } 411 412 void _Copy_arrayof_conjoint_bytes(HeapWord* from, 413 HeapWord* to, 414 size_t count) { 415 memmove(to, from, count); 416 } 417 void _Copy_arrayof_conjoint_jshorts(HeapWord* from, 418 HeapWord* to, 419 size_t count) { 420 memmove(to, from, count * 2); 421 } 422 void _Copy_arrayof_conjoint_jints(HeapWord* from, 423 HeapWord* to, 424 size_t count) { 425 memmove(to, from, count * 4); 426 } 427 void _Copy_arrayof_conjoint_jlongs(HeapWord* from, 428 HeapWord* to, 429 size_t count) { 430 memmove(to, from, count * 8); 431 } 432 }; 433 434 ///////////////////////////////////////////////////////////////////////////// 435 // Implementations of atomic operations not supported by processors. 436 // -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html 437 438 #ifndef _LP64 439 extern "C" { 440 long long unsigned int __sync_val_compare_and_swap_8( 441 volatile void *ptr, 442 long long unsigned int oldval, 443 long long unsigned int newval) { 444 ShouldNotCallThis(); 445 } 446 }; 447 #endif // !_LP64 448 449 #ifndef PRODUCT 450 void os::verify_stack_alignment() { 451 } 452 #endif 453 454 int os::extra_bang_size_in_bytes() { 455 // Zero does not require an additional stack bang. 456 return 0; 457 }