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