1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Change early.Printf calls to kfmt.Printf

This commit is contained in:
Achilleas Anagnostopoulos
2017-07-01 19:00:11 +01:00
parent 545a18fccc
commit eca1f6c26e
12 changed files with 81 additions and 174 deletions

View File

@@ -61,12 +61,3 @@ func TestTranslateAmd64(t *testing.T) {
}
}
}
/*
phys, err := vmm.Translate(uintptr(100 * mem.Mb))
if err != nil {
early.Printf("err: %s\n", err.Error())
} else {
early.Printf("phys: 0x%x\n", phys)
}
*/

View File

@@ -4,7 +4,7 @@ import (
"gopheros/kernel"
"gopheros/kernel/cpu"
"gopheros/kernel/irq"
"gopheros/kernel/kfmt/early"
"gopheros/kernel/kfmt"
"gopheros/kernel/mem"
"gopheros/kernel/mem/pmm"
)
@@ -83,27 +83,27 @@ func pageFaultHandler(errorCode uint64, frame *irq.Frame, regs *irq.Regs) {
}
func nonRecoverablePageFault(faultAddress uintptr, errorCode uint64, frame *irq.Frame, regs *irq.Regs, err *kernel.Error) {
early.Printf("\nPage fault while accessing address: 0x%16x\nReason: ", faultAddress)
kfmt.Printf("\nPage fault while accessing address: 0x%16x\nReason: ", faultAddress)
switch {
case errorCode == 0:
early.Printf("read from non-present page")
kfmt.Printf("read from non-present page")
case errorCode == 1:
early.Printf("page protection violation (read)")
kfmt.Printf("page protection violation (read)")
case errorCode == 2:
early.Printf("write to non-present page")
kfmt.Printf("write to non-present page")
case errorCode == 3:
early.Printf("page protection violation (write)")
kfmt.Printf("page protection violation (write)")
case errorCode == 4:
early.Printf("page-fault in user-mode")
kfmt.Printf("page-fault in user-mode")
case errorCode == 8:
early.Printf("page table has reserved bit set")
kfmt.Printf("page table has reserved bit set")
case errorCode == 16:
early.Printf("instruction fetch")
kfmt.Printf("instruction fetch")
default:
early.Printf("unknown")
kfmt.Printf("unknown")
}
early.Printf("\n\nRegisters:\n")
kfmt.Printf("\n\nRegisters:\n")
regs.Print()
frame.Print()
@@ -112,8 +112,8 @@ func nonRecoverablePageFault(faultAddress uintptr, errorCode uint64, frame *irq.
}
func generalProtectionFaultHandler(_ uint64, frame *irq.Frame, regs *irq.Regs) {
early.Printf("\nGeneral protection fault while accessing address: 0x%x\n", readCR2Fn())
early.Printf("Registers:\n")
kfmt.Printf("\nGeneral protection fault while accessing address: 0x%x\n", readCR2Fn())
kfmt.Printf("Registers:\n")
regs.Print()
frame.Print()

View File

@@ -5,9 +5,8 @@ import (
"fmt"
"gopheros/kernel"
"gopheros/kernel/cpu"
"gopheros/kernel/driver/video/console"
"gopheros/kernel/hal"
"gopheros/kernel/irq"
"gopheros/kernel/kfmt"
"gopheros/kernel/mem"
"gopheros/kernel/mem/pmm"
"strings"
@@ -54,8 +53,6 @@ func TestRecoverablePageFault(t *testing.T) {
{FlagPresent | FlagCopyOnWrite, nil, nil, false},
}
mockTTY()
ptePtrFn = func(entry uintptr) unsafe.Pointer { return unsafe.Pointer(&pageEntry) }
readCR2Fn = func() uint64 { return uint64(uintptr(unsafe.Pointer(&origPage[0]))) }
unmapFn = func(_ Page) *kernel.Error { return nil }
@@ -102,6 +99,10 @@ func TestRecoverablePageFault(t *testing.T) {
}
func TestNonRecoverablePageFault(t *testing.T) {
defer func() {
kfmt.SetOutputSink(nil)
}()
specs := []struct {
errCode uint64
expReason string
@@ -143,19 +144,21 @@ func TestNonRecoverablePageFault(t *testing.T) {
var (
regs irq.Regs
frame irq.Frame
buf bytes.Buffer
)
kfmt.SetOutputSink(&buf)
for specIndex, spec := range specs {
t.Run(fmt.Sprint(specIndex), func(t *testing.T) {
buf.Reset()
defer func() {
if err := recover(); err != errUnrecoverableFault {
t.Errorf("expected a panic with errUnrecoverableFault; got %v", err)
}
}()
fb := mockTTY()
nonRecoverablePageFault(0xbadf00d000, spec.errCode, &frame, &regs, errUnrecoverableFault)
if got := readTTY(fb); !strings.Contains(got, spec.expReason) {
if got := buf.String(); !strings.Contains(got, spec.expReason) {
t.Errorf("expected reason %q; got output:\n%q", spec.expReason, got)
}
})
@@ -182,7 +185,6 @@ func TestGPtHandler(t *testing.T) {
}
}()
mockTTY()
generalProtectionFaultHandler(0, &frame, &regs)
}
@@ -252,30 +254,3 @@ func TestInit(t *testing.T) {
}
})
}
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
}