// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import 

type sigctxt struct {
	info *siginfo
	ctxt unsafe.Pointer
}

//go:nosplit
//go:nowritebarrierrec
func ( *sigctxt) () *regs64 { return &(*ucontext)(.ctxt).uc_mcontext.ss }

func ( *sigctxt) () uint64 { return .regs().rax }
func ( *sigctxt) () uint64 { return .regs().rbx }
func ( *sigctxt) () uint64 { return .regs().rcx }
func ( *sigctxt) () uint64 { return .regs().rdx }
func ( *sigctxt) () uint64 { return .regs().rdi }
func ( *sigctxt) () uint64 { return .regs().rsi }
func ( *sigctxt) () uint64 { return .regs().rbp }
func ( *sigctxt) () uint64 { return .regs().rsp }
func ( *sigctxt) () uint64  { return .regs().r8 }
func ( *sigctxt) () uint64  { return .regs().r9 }
func ( *sigctxt) () uint64 { return .regs().r10 }
func ( *sigctxt) () uint64 { return .regs().r11 }
func ( *sigctxt) () uint64 { return .regs().r12 }
func ( *sigctxt) () uint64 { return .regs().r13 }
func ( *sigctxt) () uint64 { return .regs().r14 }
func ( *sigctxt) () uint64 { return .regs().r15 }

//go:nosplit
//go:nowritebarrierrec
func ( *sigctxt) () uint64 { return .regs().rip }

func ( *sigctxt) () uint64  { return .regs().rflags }
func ( *sigctxt) () uint64      { return .regs().cs }
func ( *sigctxt) () uint64      { return .regs().fs }
func ( *sigctxt) () uint64      { return .regs().gs }
func ( *sigctxt) () uint64 { return uint64(.info.si_code) }
func ( *sigctxt) () uint64 { return .info.si_addr }

func ( *sigctxt) ( uint64)     { .regs().rip =  }
func ( *sigctxt) ( uint64)     { .regs().rsp =  }
func ( *sigctxt) ( uint64) { .info.si_code = int32() }
func ( *sigctxt) ( uint64) { .info.si_addr =  }

//go:nosplit
func ( *sigctxt) ( uint32) {
	switch  {
	case _SIGTRAP:
		// OS X sets c.sigcode() == TRAP_BRKPT unconditionally for all SIGTRAPs,
		// leaving no way to distinguish a breakpoint-induced SIGTRAP
		// from an asynchronous signal SIGTRAP.
		// They all look breakpoint-induced by default.
		// Try looking at the code to see if it's a breakpoint.
		// The assumption is that we're very unlikely to get an
		// asynchronous SIGTRAP at just the moment that the
		// PC started to point at unmapped memory.
		 := uintptr(.rip())
		// OS X will leave the pc just after the INT 3 instruction.
		// INT 3 is usually 1 byte, but there is a 2-byte form.
		 := (*[2]byte)(unsafe.Pointer( - 2))
		if [1] != 0xCC && ([0] != 0xCD || [1] != 3) {
			// SIGTRAP on something other than INT 3.
			.set_sigcode(_SI_USER)
		}

	case _SIGSEGV:
		// x86-64 has 48-bit virtual addresses. The top 16 bits must echo bit 47.
		// The hardware delivers a different kind of fault for a malformed address
		// than it does for an attempt to access a valid but unmapped address.
		// OS X 10.9.2 mishandles the malformed address case, making it look like
		// a user-generated signal (like someone ran kill -SEGV ourpid).
		// We pass user-generated signals to os/signal, or else ignore them.
		// Doing that here - and returning to the faulting code - results in an
		// infinite loop. It appears the best we can do is rewrite what the kernel
		// delivers into something more like the truth. The address used below
		// has very little chance of being the one that caused the fault, but it is
		// malformed, it is clearly not a real pointer, and if it does get printed
		// in real life, people will probably search for it and find this code.
		// There are no Google hits for b01dfacedebac1e or 0xb01dfacedebac1e
		// as I type this comment.
		if .sigcode() == _SI_USER {
			.set_sigcode(_SI_USER + 1)
			.set_sigaddr(0xb01dfacedebac1e)
		}
	}
}