1
0
mirror of https://github.com/taigrr/gopher-os synced 2026-04-01 08:18:42 -07:00

Move kernel panic implementation to the kfmt package

This commit is contained in:
Achilleas Anagnostopoulos
2017-07-06 06:56:22 +01:00
parent dc37e86421
commit d8793bd530
2 changed files with 8 additions and 6 deletions

View File

@@ -0,0 +1,49 @@
package kfmt
import (
"gopheros/kernel"
"gopheros/kernel/cpu"
"gopheros/kernel/kfmt/early"
)
var (
// cpuHaltFn is mocked by tests and is automatically inlined by the compiler.
cpuHaltFn = cpu.Halt
errRuntimePanic = &kernel.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 *kernel.Error
switch t := e.(type) {
case *kernel.Error:
err = t
case string:
panicString(t)
return
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()
}
// panicString serves as a redirect target for runtime.throw
//go:redirect-from runtime.throw
func panicString(msg string) {
errRuntimePanic.Message = msg
Panic(errRuntimePanic)
}

View File

@@ -0,0 +1,121 @@
package kfmt
import (
"bytes"
"errors"
"gopheros/kernel"
"gopheros/kernel/cpu"
"gopheros/kernel/driver/video/console"
"gopheros/kernel/hal"
"testing"
"unsafe"
)
func TestPanic(t *testing.T) {
defer func() {
cpuHaltFn = cpu.Halt
}()
var cpuHaltCalled bool
cpuHaltFn = func() {
cpuHaltCalled = true
}
t.Run("with *kernel.Error", func(t *testing.T) {
cpuHaltCalled = false
fb := mockTTY()
err := &kernel.Error{Module: "test", Message: "panic test"}
Panic(err)
exp := "\n-----------------------------------\n[test] unrecoverable error: panic test\n*** kernel panic: system halted ***\n-----------------------------------"
if got := readTTY(fb); got != exp {
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
}
if !cpuHaltCalled {
t.Fatal("expected cpu.Halt() to be called by Panic")
}
})
t.Run("with error", func(t *testing.T) {
cpuHaltCalled = false
fb := mockTTY()
err := errors.New("go error")
Panic(err)
exp := "\n-----------------------------------\n[rt] unrecoverable error: go error\n*** kernel panic: system halted ***\n-----------------------------------"
if got := readTTY(fb); got != exp {
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
}
if !cpuHaltCalled {
t.Fatal("expected cpu.Halt() to be called by Panic")
}
})
t.Run("with string", func(t *testing.T) {
cpuHaltCalled = false
fb := mockTTY()
err := "string error"
Panic(err)
exp := "\n-----------------------------------\n[rt] unrecoverable error: string error\n*** kernel panic: system halted ***\n-----------------------------------"
if got := readTTY(fb); got != exp {
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
}
if !cpuHaltCalled {
t.Fatal("expected cpu.Halt() to be called by Panic")
}
})
t.Run("without error", func(t *testing.T) {
cpuHaltCalled = false
fb := mockTTY()
Panic(nil)
exp := "\n-----------------------------------\n*** kernel panic: system halted ***\n-----------------------------------"
if got := readTTY(fb); got != exp {
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
}
if !cpuHaltCalled {
t.Fatal("expected cpu.Halt() to be called by Panic")
}
})
}
func readTTY(fb []byte) string {
var buf bytes.Buffer
for i := 0; i < len(fb); i += 2 {
ch := fb[i]
if ch == 0 {
if i+2 < len(fb) && fb[i+2] != 0 {
buf.WriteByte('\n')
}
continue
}
buf.WriteByte(ch)
}
return buf.String()
}
func mockTTY() []byte {
// Mock a tty to handle early.Printf output
mockConsoleFb := make([]byte, 160*25)
mockConsole := &console.Ega{}
mockConsole.Init(80, 25, uintptr(unsafe.Pointer(&mockConsoleFb[0])))
hal.ActiveTerminal.AttachTo(mockConsole)
return mockConsoleFb
}