1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00
gopher-os/kernel/panic.go
Achilleas Anagnostopoulos 5fc6ce188e Use go:redirect-from directive to map panic to kernel.Panic
All calls (but one) to kernel.Panic have been replaced by calls to
panic. A call to kernel.Panic is still required to prevent the compiler
from treating kernel.Panic as dead code and eliminating it.
2017-06-25 21:39:56 +01:00

42 lines
1.0 KiB
Go

package kernel
import (
"github.com/achilleasa/gopher-os/kernel/cpu"
"github.com/achilleasa/gopher-os/kernel/kfmt/early"
)
var (
// cpuHaltFn is mocked by tests and is automatically inlined by the compiler.
cpuHaltFn = cpu.Halt
errRuntimePanic = &Error{Module: "rt", Message: "unknown cause"}
)
// Panic outputs the supplied error (if not nil) to the console and halts the
// CPU. Calls to Panic never return. Panic also works as a redirection target
// for calls to panic() (resolved via runtime.gopanic)
//go:redirect-from runtime.gopanic
func Panic(e interface{}) {
var err *Error
switch t := e.(type) {
case *Error:
err = t
case string:
errRuntimePanic.Message = t
err = errRuntimePanic
case error:
errRuntimePanic.Message = t.Error()
err = errRuntimePanic
}
early.Printf("\n-----------------------------------\n")
if err != nil {
early.Printf("[%s] unrecoverable error: %s\n", err.Module, err.Message)
}
early.Printf("*** kernel panic: system halted ***")
early.Printf("\n-----------------------------------\n")
cpuHaltFn()
}