1 /*
2  * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2016, 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 // This file is organized as os_linux_x86.cpp.
27 
28 // no precompiled headers
29 #include "jvm.h"
30 #include "asm/assembler.inline.hpp"
31 #include "classfile/classLoader.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "code/icBuffer.hpp"
35 #include "code/nativeInst.hpp"
36 #include "code/vtableStubs.hpp"
37 #include "compiler/disassembler.hpp"
38 #include "interpreter/interpreter.hpp"
39 #include "memory/allocation.inline.hpp"
40 #include "nativeInst_s390.hpp"
41 #include "os_share_linux.hpp"
42 #include "prims/jniFastGetField.hpp"
43 #include "prims/jvm_misc.hpp"
44 #include "runtime/arguments.hpp"
45 #include "runtime/extendedPC.hpp"
46 #include "runtime/frame.inline.hpp"
47 #include "runtime/interfaceSupport.inline.hpp"
48 #include "runtime/java.hpp"
49 #include "runtime/javaCalls.hpp"
50 #include "runtime/mutexLocker.hpp"
51 #include "runtime/osThread.hpp"
52 #include "runtime/safepointMechanism.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/debug.hpp"
59 #include "utilities/vmError.hpp"
60 
61 // put OS-includes here
62 # include <sys/types.h>
63 # include <sys/mman.h>
64 # include <pthread.h>
65 # include <signal.h>
66 # include <errno.h>
67 # include <dlfcn.h>
68 # include <stdlib.h>
69 # include <stdio.h>
70 # include <unistd.h>
71 # include <sys/resource.h>
72 # include <pthread.h>
73 # include <sys/stat.h>
74 # include <sys/time.h>
75 # include <sys/utsname.h>
76 # include <sys/socket.h>
77 # include <sys/wait.h>
78 # include <pwd.h>
79 # include <poll.h>
80 # include <ucontext.h>
81 
current_stack_pointer()82 address os::current_stack_pointer() {
83   intptr_t* csp;
84 
85   // Inline assembly for `z_lgr regno(csp), Z_SP' (Z_SP = Z_R15):
86   __asm__ __volatile__ ("lgr %0, 15":"=r"(csp):);
87 
88   assert(((uint64_t)csp & (frame::alignment_in_bytes-1)) == 0, "SP must be aligned");
89   return (address) csp;
90 }
91 
non_memory_address_word()92 char* os::non_memory_address_word() {
93   // Must never look like an address returned by reserve_memory,
94   // even in its subfields (as defined by the CPU immediate fields,
95   // if the CPU splits constants across multiple instructions).
96   return (char*) -1;
97 }
98 
99 // Frame information (pc, sp, fp) retrieved via ucontext
100 // always looks like a C-frame according to the frame
101 // conventions in frame_s390.hpp.
ucontext_get_pc(const ucontext_t * uc)102 address os::Linux::ucontext_get_pc(const ucontext_t * uc) {
103   return (address)uc->uc_mcontext.psw.addr;
104 }
105 
ucontext_set_pc(ucontext_t * uc,address pc)106 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) {
107   uc->uc_mcontext.psw.addr = (unsigned long)pc;
108 }
109 
ucontext_get_lr(const ucontext_t * uc)110 static address ucontext_get_lr(const ucontext_t * uc) {
111   return (address)uc->uc_mcontext.gregs[14/*LINK*/];
112 }
113 
ucontext_get_sp(const ucontext_t * uc)114 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
115   return (intptr_t*)uc->uc_mcontext.gregs[15/*REG_SP*/];
116 }
117 
ucontext_get_fp(const ucontext_t * uc)118 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
119   return NULL;
120 }
121 
fetch_frame_from_context(const void * ucVoid,intptr_t ** ret_sp,intptr_t ** ret_fp)122 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
123                     intptr_t** ret_sp, intptr_t** ret_fp) {
124 
125   ExtendedPC  epc;
126   const ucontext_t* uc = (const ucontext_t*)ucVoid;
127 
128   if (uc != NULL) {
129     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
130     if (ret_sp) { *ret_sp = os::Linux::ucontext_get_sp(uc); }
131     if (ret_fp) { *ret_fp = os::Linux::ucontext_get_fp(uc); }
132   } else {
133     // Construct empty ExtendedPC for return value checking.
134     epc = ExtendedPC(NULL);
135     if (ret_sp) { *ret_sp = (intptr_t *)NULL; }
136     if (ret_fp) { *ret_fp = (intptr_t *)NULL; }
137   }
138 
139   return epc;
140 }
141 
fetch_frame_from_context(const void * ucVoid)142 frame os::fetch_frame_from_context(const void* ucVoid) {
143   intptr_t* sp;
144   intptr_t* fp;
145   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
146   return frame(sp, epc.pc());
147 }
148 
get_frame_at_stack_banging_point(JavaThread * thread,ucontext_t * uc,frame * fr)149 bool os::Linux::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
150   address pc = (address) os::Linux::ucontext_get_pc(uc);
151   if (Interpreter::contains(pc)) {
152     // Interpreter performs stack banging after the fixed frame header has
153     // been generated while the compilers perform it before. To maintain
154     // semantic consistency between interpreted and compiled frames, the
155     // method returns the Java sender of the current frame.
156     *fr = os::fetch_frame_from_context(uc);
157     if (!fr->is_first_java_frame()) {
158       assert(fr->safe_for_sender(thread), "Safety check");
159       *fr = fr->java_sender();
160     }
161   } else {
162     // More complex code with compiled code.
163     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
164     CodeBlob* cb = CodeCache::find_blob(pc);
165     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
166       // Not sure where the pc points to, fallback to default
167       // stack overflow handling. In compiled code, we bang before
168       // the frame is complete.
169       return false;
170     } else {
171       intptr_t* sp = os::Linux::ucontext_get_sp(uc);
172       address lr = ucontext_get_lr(uc);
173       *fr = frame(sp, lr);
174       if (!fr->is_java_frame()) {
175         assert(fr->safe_for_sender(thread), "Safety check");
176         assert(!fr->is_first_frame(), "Safety check");
177         *fr = fr->java_sender();
178       }
179     }
180   }
181   assert(fr->is_java_frame(), "Safety check");
182   return true;
183 }
184 
get_sender_for_C_frame(frame * fr)185 frame os::get_sender_for_C_frame(frame* fr) {
186   if (*fr->sp() == 0) {
187     // fr is the last C frame.
188     return frame();
189   }
190 
191   // If its not one of our frames, the return pc is saved at gpr14
192   // stack slot. The call_stub stores the return_pc to the stack slot
193   // of gpr10.
194   if ((Interpreter::code() != NULL && Interpreter::contains(fr->pc())) ||
195       (CodeCache::contains(fr->pc()) && !StubRoutines::contains(fr->pc()))) {
196     return frame(fr->sender_sp(), fr->sender_pc());
197   } else {
198     if (StubRoutines::contains(fr->pc())) {
199       StubCodeDesc* desc = StubCodeDesc::desc_for(fr->pc());
200       if (desc && !strcmp(desc->name(),"call_stub")) {
201         return frame(fr->sender_sp(), fr->callstub_sender_pc());
202       } else {
203         return frame(fr->sender_sp(), fr->sender_pc());
204       }
205     } else {
206       return frame(fr->sender_sp(), fr->native_sender_pc());
207     }
208   }
209 }
210 
current_frame()211 frame os::current_frame() {
212   // Expected to return the stack pointer of this method.
213   // But if inlined, returns the stack pointer of our caller!
214   intptr_t* csp = (intptr_t*) *((intptr_t*) os::current_stack_pointer());
215   assert (csp != NULL, "sp should not be NULL");
216   // Pass a dummy pc. This way we don't have to load it from the
217   // stack, since we don't know in which slot we can find it.
218   frame topframe(csp, (address)0x8);
219   if (os::is_first_C_frame(&topframe)) {
220     // Stack is not walkable.
221     return frame();
222   } else {
223     frame senderFrame = os::get_sender_for_C_frame(&topframe);
224     assert(senderFrame.pc() != NULL, "Sender pc should not be NULL");
225     // Return sender of sender of current topframe which hopefully
226     // both have pc != NULL.
227 #ifdef _NMT_NOINLINE_   // Is set in slowdebug builds.
228     // Current_stack_pointer is not inlined, we must pop one more frame.
229     frame tmp = os::get_sender_for_C_frame(&topframe);
230     return os::get_sender_for_C_frame(&tmp);
231 #else
232     return os::get_sender_for_C_frame(&topframe);
233 #endif
234   }
235 }
236 
237 // Utility functions
238 
239 extern "C" JNIEXPORT int
JVM_handle_linux_signal(int sig,siginfo_t * info,void * ucVoid,int abort_if_unrecognized)240 JVM_handle_linux_signal(int sig,
241                         siginfo_t* info,
242                         void* ucVoid,
243                         int abort_if_unrecognized) {
244   ucontext_t* uc = (ucontext_t*) ucVoid;
245 
246   Thread* t = Thread::current_or_null_safe();
247 
248   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
249   // (no destructors can be run).
250   os::ThreadCrashProtection::check_crash_protection(sig, t);
251 
252   SignalHandlerMark shm(t);
253 
254   // Note: it's not uncommon that JNI code uses signal/sigset to install
255   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
256   // or have a SIGILL handler when detecting CPU type). When that happens,
257   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
258   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
259   // that do not require siginfo/ucontext first.
260 
261   if (sig == SIGPIPE) {
262     if (os::Linux::chained_handler(sig, info, ucVoid)) {
263       return true;
264     } else {
265       if (PrintMiscellaneous && (WizardMode || Verbose)) {
266         warning("Ignoring SIGPIPE - see bug 4229104");
267       }
268       return true;
269     }
270   }
271 
272 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
273   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
274     if (handle_assert_poison_fault(ucVoid, info->si_addr)) {
275       return 1;
276     }
277   }
278 #endif
279 
280   JavaThread* thread = NULL;
281   VMThread* vmthread = NULL;
282   if (os::Linux::signal_handlers_are_installed) {
283     if (t != NULL) {
284       if(t->is_Java_thread()) {
285         thread = (JavaThread*)t;
286       } else if(t->is_VM_thread()) {
287         vmthread = (VMThread *)t;
288       }
289     }
290   }
291 
292   // Moved SafeFetch32 handling outside thread!=NULL conditional block to make
293   // it work if no associated JavaThread object exists.
294   if (uc) {
295     address const pc = os::Linux::ucontext_get_pc(uc);
296     if (pc && StubRoutines::is_safefetch_fault(pc)) {
297       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
298       return true;
299     }
300   }
301 
302   // Decide if this trap can be handled by a stub.
303   address stub    = NULL;
304   address pc      = NULL;  // Pc as retrieved from PSW. Usually points past failing instruction.
305   address trap_pc = NULL;  // Pc of the instruction causing the trap.
306 
307   //%note os_trap_1
308   if (info != NULL && uc != NULL && thread != NULL) {
309     pc = os::Linux::ucontext_get_pc(uc);
310     if (TraceTraps) {
311       tty->print_cr("     pc at " INTPTR_FORMAT, p2i(pc));
312     }
313     if ((unsigned long)(pc - (address)info->si_addr) <= (unsigned long)Assembler::instr_maxlen() ) {
314       trap_pc = (address)info->si_addr;
315       if (TraceTraps) {
316         tty->print_cr("trap_pc at " INTPTR_FORMAT, p2i(trap_pc));
317       }
318     }
319 
320     // Handle ALL stack overflow variations here
321     if (sig == SIGSEGV) {
322       address addr = (address)info->si_addr; // Address causing SIGSEGV, usually mem ref target.
323 
324       // Check if fault address is within thread stack.
325       if (thread->is_in_full_stack(addr)) {
326         // stack overflow
327         if (thread->in_stack_yellow_reserved_zone(addr)) {
328           if (thread->thread_state() == _thread_in_Java) {
329             if (thread->in_stack_reserved_zone(addr)) {
330               frame fr;
331               if (os::Linux::get_frame_at_stack_banging_point(thread, uc, &fr)) {
332                 assert(fr.is_java_frame(), "Must be a Javac frame");
333                 frame activation =
334                   SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
335                 if (activation.sp() != NULL) {
336                   thread->disable_stack_reserved_zone();
337                   if (activation.is_interpreted_frame()) {
338                     thread->set_reserved_stack_activation((address)activation.fp());
339                   } else {
340                     thread->set_reserved_stack_activation((address)activation.unextended_sp());
341                   }
342                   return 1;
343                 }
344               }
345             }
346             // Throw a stack overflow exception.
347             // Guard pages will be reenabled while unwinding the stack.
348             thread->disable_stack_yellow_reserved_zone();
349             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
350           } else {
351             // Thread was in the vm or native code. Return and try to finish.
352             thread->disable_stack_yellow_reserved_zone();
353             return 1;
354           }
355         } else if (thread->in_stack_red_zone(addr)) {
356           // Fatal red zone violation.  Disable the guard pages and fall through
357           // to handle_unexpected_exception way down below.
358           thread->disable_stack_red_zone();
359           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
360 
361           // This is a likely cause, but hard to verify. Let's just print
362           // it as a hint.
363           tty->print_raw_cr("Please check if any of your loaded .so files has "
364                             "enabled executable stack (see man page execstack(8))");
365         } else {
366           // Accessing stack address below sp may cause SEGV if current
367           // thread has MAP_GROWSDOWN stack. This should only happen when
368           // current thread was created by user code with MAP_GROWSDOWN flag
369           // and then attached to VM. See notes in os_linux.cpp.
370           if (thread->osthread()->expanding_stack() == 0) {
371              thread->osthread()->set_expanding_stack();
372              if (os::Linux::manually_expand_stack(thread, addr)) {
373                thread->osthread()->clear_expanding_stack();
374                return 1;
375              }
376              thread->osthread()->clear_expanding_stack();
377           } else {
378              fatal("recursive segv. expanding stack.");
379           }
380         }
381       }
382     }
383 
384     if (thread->thread_state() == _thread_in_Java) {
385       // Java thread running in Java code => find exception handler if any
386       // a fault inside compiled code, the interpreter, or a stub
387 
388       // Handle signal from NativeJump::patch_verified_entry().
389       if (sig == SIGILL && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
390         if (TraceTraps) {
391           tty->print_cr("trap: zombie_not_entrant (SIGILL)");
392         }
393         stub = SharedRuntime::get_handle_wrong_method_stub();
394       }
395 
396       else if (sig == SIGSEGV &&
397                SafepointMechanism::is_poll_address((address)info->si_addr)) {
398         if (TraceTraps) {
399           tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
400         }
401         stub = SharedRuntime::get_poll_stub(pc);
402 
403         // Info->si_addr only points to the page base address, so we
404         // must extract the real si_addr from the instruction and the
405         // ucontext.
406         assert(((NativeInstruction*)pc)->is_safepoint_poll(), "must be safepoint poll");
407         const address real_si_addr = ((NativeInstruction*)pc)->get_poll_address(uc);
408       }
409 
410       // SIGTRAP-based implicit null check in compiled code.
411       else if ((sig == SIGFPE) &&
412                TrapBasedNullChecks &&
413                (trap_pc != NULL) &&
414                Assembler::is_sigtrap_zero_check(trap_pc)) {
415         if (TraceTraps) {
416           tty->print_cr("trap: NULL_CHECK at " INTPTR_FORMAT " (SIGFPE)", p2i(trap_pc));
417         }
418         stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_NULL);
419       }
420 
421       else if (sig == SIGSEGV && ImplicitNullChecks &&
422                CodeCache::contains((void*) pc) &&
423                MacroAssembler::uses_implicit_null_check(info->si_addr)) {
424         if (TraceTraps) {
425           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
426         }
427         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
428       }
429 
430 #ifdef COMPILER2
431       // SIGTRAP-based implicit range check in compiled code.
432       else if (sig == SIGFPE && TrapBasedRangeChecks &&
433                (trap_pc != NULL) &&
434                Assembler::is_sigtrap_range_check(trap_pc)) {
435         if (TraceTraps) {
436           tty->print_cr("trap: RANGE_CHECK at " INTPTR_FORMAT " (SIGFPE)", p2i(trap_pc));
437         }
438         stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_NULL);
439       }
440 #endif
441 
442       else if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
443         stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
444       }
445 
446       else if (sig == SIGBUS) {
447         // BugId 4454115: A read from a MappedByteBuffer can fault here if the
448         // underlying file has been truncated. Do not crash the VM in such a case.
449         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
450         CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
451         if (nm != NULL && nm->has_unsafe_access()) {
452           // We don't really need a stub here! Just set the pending exeption and
453           // continue at the next instruction after the faulting read. Returning
454           // garbage from this read is ok.
455           thread->set_pending_unsafe_access_error();
456           uc->uc_mcontext.psw.addr = ((unsigned long)pc) + Assembler::instr_len(pc);
457           return true;
458         }
459       }
460     }
461 
462     else { // thread->thread_state() != _thread_in_Java
463       if ((sig == SIGILL) && VM_Version::is_determine_features_test_running()) {
464         // SIGILL must be caused by VM_Version::determine_features()
465         // when attempting to execute a non-existing instruction.
466         //*(int *) (pc-6)=0; // Patch instruction to 0 to indicate that it causes a SIGILL.
467                              // Flushing of icache is not necessary.
468         stub = pc; // Continue with next instruction.
469       } else if ((sig == SIGFPE) && VM_Version::is_determine_features_test_running()) {
470         // SIGFPE is known to be caused by trying to execute a vector instruction
471         // when the vector facility is installed, but operating system support is missing.
472         VM_Version::reset_has_VectorFacility();
473         stub = pc; // Continue with next instruction.
474       } else if ((thread->thread_state() == _thread_in_vm ||
475                   thread->thread_state() == _thread_in_native) &&
476                  sig == SIGBUS && thread->doing_unsafe_access()) {
477         // We don't really need a stub here! Just set the pending exeption and
478         // continue at the next instruction after the faulting read. Returning
479         // garbage from this read is ok.
480         thread->set_pending_unsafe_access_error();
481         os::Linux::ucontext_set_pc(uc, pc + Assembler::instr_len(pc));
482         return true;
483       }
484     }
485 
486     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
487     // and the heap gets shrunk before the field access.
488     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
489       address addr = JNI_FastGetField::find_slowcase_pc(pc);
490       if (addr != (address)-1) {
491         stub = addr;
492       }
493     }
494   }
495 
496   if (stub != NULL) {
497     // Save all thread context in case we need to restore it.
498     if (thread != NULL) thread->set_saved_exception_pc(pc);
499     os::Linux::ucontext_set_pc(uc, stub);
500     return true;
501   }
502 
503   // signal-chaining
504   if (os::Linux::chained_handler(sig, info, ucVoid)) {
505     return true;
506   }
507 
508   if (!abort_if_unrecognized) {
509     // caller wants another chance, so give it to him
510     return false;
511   }
512 
513   if (pc == NULL && uc != NULL) {
514     pc = os::Linux::ucontext_get_pc(uc);
515   }
516 
517   // unmask current signal
518   sigset_t newset;
519   sigemptyset(&newset);
520   sigaddset(&newset, sig);
521   sigprocmask(SIG_UNBLOCK, &newset, NULL);
522 
523   // Hand down correct pc for SIGILL, SIGFPE. pc from context
524   // usually points to the instruction after the failing instruction.
525   // Note: this should be combined with the trap_pc handling above,
526   // because it handles the same issue.
527   if (sig == SIGILL || sig == SIGFPE) {
528     pc = (address)info->si_addr;
529   }
530 
531   VMError::report_and_die(t, sig, pc, info, ucVoid);
532 
533   ShouldNotReachHere();
534   return false;
535 }
536 
init_thread_fpu_state(void)537 void os::Linux::init_thread_fpu_state(void) {
538   // Nothing to do on z/Architecture.
539 }
540 
get_fpu_control_word(void)541 int os::Linux::get_fpu_control_word(void) {
542   // Nothing to do on z/Architecture.
543   return 0;
544 }
545 
set_fpu_control_word(int fpu_control)546 void os::Linux::set_fpu_control_word(int fpu_control) {
547   // Nothing to do on z/Architecture.
548 }
549 
550 ////////////////////////////////////////////////////////////////////////////////
551 // thread stack
552 
553 // Minimum usable stack sizes required to get to user code. Space for
554 // HotSpot guard pages is added later.
555 size_t os::Posix::_compiler_thread_min_stack_allowed = (52 DEBUG_ONLY(+ 32)) * K;
556 size_t os::Posix::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 8)) * K;
557 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 32 * K;
558 
559 // Return default stack size for thr_type.
default_stack_size(os::ThreadType thr_type)560 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
561   // Default stack size (compiler thread needs larger stack).
562   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);
563   return s;
564 }
565 
566 /////////////////////////////////////////////////////////////////////////////
567 // helper functions for fatal error handler
568 
print_context(outputStream * st,const void * context)569 void os::print_context(outputStream *st, const void *context) {
570   if (context == NULL) return;
571 
572   const ucontext_t* uc = (const ucontext_t*)context;
573 
574   st->print_cr("Processor state:");
575   st->print_cr("----------------");
576   st->print_cr("        ip = " INTPTR_FORMAT " ", uc->uc_mcontext.psw.addr);
577   st->print_cr(" proc mask = " INTPTR_FORMAT " ", uc->uc_mcontext.psw.mask);
578   st->print_cr("   fpc reg = 0x%8.8x "          , uc->uc_mcontext.fpregs.fpc);
579   st->cr();
580 
581   st->print_cr("General Purpose Registers:");
582   st->print_cr("--------------------------");
583   for( int i = 0; i < 16; i+=2 ) {
584     st->print("  r%-2d = " INTPTR_FORMAT "  " ,  i,   uc->uc_mcontext.gregs[i]);
585     st->print("  r%-2d = " INTPTR_FORMAT "  |",  i+1, uc->uc_mcontext.gregs[i+1]);
586     st->print("  r%-2d = %23.1ld  "           ,  i,   uc->uc_mcontext.gregs[i]);
587     st->print("  r%-2d = %23.1ld  "           ,  i+1, uc->uc_mcontext.gregs[i+1]);
588     st->cr();
589   }
590   st->cr();
591 
592   st->print_cr("Access Registers:");
593   st->print_cr("-----------------");
594   for( int i = 0; i < 16; i+=2 ) {
595     st->print("  ar%-2d = 0x%8.8x  ", i,   uc->uc_mcontext.aregs[i]);
596     st->print("  ar%-2d = 0x%8.8x  ", i+1, uc->uc_mcontext.aregs[i+1]);
597     st->cr();
598   }
599   st->cr();
600 
601   st->print_cr("Float Registers:");
602   st->print_cr("----------------");
603   for (int i = 0; i < 16; i += 2) {
604     st->print("  fr%-2d = " INTPTR_FORMAT "  " , i,   (int64_t)(uc->uc_mcontext.fpregs.fprs[i].d));
605     st->print("  fr%-2d = " INTPTR_FORMAT "  |", i+1, (int64_t)(uc->uc_mcontext.fpregs.fprs[i+1].d));
606     st->print("  fr%-2d = %23.15e  "           , i,   (uc->uc_mcontext.fpregs.fprs[i].d));
607     st->print("  fr%-2d = %23.15e  "           , i+1, (uc->uc_mcontext.fpregs.fprs[i+1].d));
608     st->cr();
609   }
610   st->cr();
611   st->cr();
612 
613   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
614   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
615   print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));
616   st->cr();
617 
618   // Note: it may be unsafe to inspect memory near pc. For example, pc may
619   // point to garbage if entry point in an nmethod is corrupted. Leave
620   // this at the end, and hope for the best.
621   address pc = os::Linux::ucontext_get_pc(uc);
622   print_instructions(st, pc, /*intrsize=*/4);
623   st->cr();
624 }
625 
print_register_info(outputStream * st,const void * context)626 void os::print_register_info(outputStream *st, const void *context) {
627   if (context == NULL) return;
628 
629   const ucontext_t *uc = (const ucontext_t*)context;
630 
631   st->print_cr("Register to memory mapping:");
632   st->cr();
633 
634   st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.psw.addr);
635   for (int i = 0; i < 16; i++) {
636     st->print("r%-2d=", i);
637     print_location(st, uc->uc_mcontext.gregs[i]);
638   }
639   st->cr();
640 }
641 
642 #ifndef PRODUCT
verify_stack_alignment()643 void os::verify_stack_alignment() {
644 }
645 #endif
646 
extra_bang_size_in_bytes()647 int os::extra_bang_size_in_bytes() {
648   // z/Architecture does not require the additional stack bang.
649   return 0;
650 }
651