Skip to content

Commit

Permalink
add fpu support (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
icexin authored Jul 11, 2021
1 parent d4991da commit a10ba64
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/phy/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func addBox(space *cp.Space) {
body.SetAngularVelocity(v)
body.SetAngle(math.Atan2(pos.Y, pos.X))

//shape := space.AddShape(cp.NewPolyShape(body, 4, verts, cp.NewTransformIdentity(), 0))
shape := space.AddShape(cp.NewCircle(body, float64(size), cp.Vector{0, 0}))
shape := space.AddShape(cp.NewPolyShape(body, 4, verts, cp.NewTransformIdentity(), 0))
// shape := space.AddShape(cp.NewCircle(body, float64(size), cp.Vector{0, 0}))
shape.SetElasticity(0)
shape.SetFriction(0.7)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (g *Game) Init() {
planetBody = g.space.AddBody(cp.NewKinematicBody())
planetBody.SetAngularVelocity(0.2)

N := 1
N := 50
for i := 0; i < N; i++ {
addBox(g.space)
}
Expand Down
4 changes: 2 additions & 2 deletions bochsrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ vga: extension=vbe
#floppya: image="images/floppy.img", status=inserted
#floppyb: image="images/b.img", status=inserted
#ata0-master: type=disk, path=os.img, cylinders=20, heads=16, spt=63
ata0-master: type=disk, path=os.img
boot: c
ata0-master: type=cdrom, path=eggos.iso, status=inserted
boot: cdrom

com1: enabled=1,mode=file,dev="serial"
mouse: enabled=0
Expand Down
15 changes: 14 additions & 1 deletion kernel/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ type TrapFrame struct {
}

type Thread struct {
// position of tf must be synced with trap.s
// position of tf and fpstate must be synced with trap.s
stack uintptr
tf *TrapFrame
kstack uintptr

// the state of fpu
fpstate uintptr

sigstack stackt
sigset sigset

Expand Down Expand Up @@ -106,6 +109,7 @@ func allocThread() *Thread {
t.sigstack.ss_size = mm.PGSIZE
t.state = INITING
t.kstack = mm.Mmap(0, _THREAD_STACK_SIZE) + _THREAD_STACK_SIZE
t.fpstate = mm.Alloc()
return t
}

Expand Down Expand Up @@ -160,6 +164,9 @@ func thread0_init() {
sp -= unsafe.Sizeof(TrapFrame{})
tf := (*TrapFrame)(unsafe.Pointer(sp))

// Because trapret restore fpstate
// we need a valid fpstate here
sys.Fxsave(t.fpstate)
tf.DS = _UDATA_IDX<<3 | _RPL_USER
tf.ES = _UDATA_IDX<<3 | _RPL_USER
tf.FS = _KTLS_IDX<<3 | _RPL_USER
Expand Down Expand Up @@ -225,6 +232,12 @@ func clone(pc, usp uintptr) int {
sp -= unsafe.Sizeof(TrapFrame{})
tf := (*TrapFrame)(unsafe.Pointer(sp))
*tf = *my.tf

// copy fpstate
fpsrc := (*[512]byte)(unsafe.Pointer(my.fpstate))
fpdst := (*[512]byte)(unsafe.Pointer(chld.fpstate))
*fpdst = *fpsrc

tf.SP = usp
tf.IP = pc
tf.AX = 0
Expand Down
10 changes: 10 additions & 0 deletions kernel/trap.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ TEXT ·idt_init(SB), NOSPLIT, $0

#define m_stack 0
#define m_tf 4
#define m_fpstate 12

TEXT alltraps(SB), NOSPLIT, $0
PUSHAL
PUSHW DS
PUSHW ES
PUSHW FS
PUSHW GS


MOVL 0(FS), CX // CX store mythread
MOVL SP, m_tf(CX)

// save FPU
MOVL m_fpstate(CX), DX
FXSAVE (DX)

// call trap(tf)
PUSHL SP
CALL ·dotrap(SB)
Expand All @@ -28,6 +34,10 @@ TEXT ·trapret(SB), NOSPLIT, $0
MOVL 0(FS), CX // CX store mythread
MOVL $0, m_tf(CX) // clear tf

// restore FPU
MOVL m_fpstate(CX), DX
FXRSTOR (DX)

POPW GS
POPW FS
POPW ES
Expand Down
2 changes: 1 addition & 1 deletion magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
)

var (
GOTAGS = "nes"
GOTAGS = "nes phy"
GOGCFLAGS = ""
)

Expand Down
3 changes: 3 additions & 0 deletions sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ func FuncPC(f interface{}) uintptr {

//go:nosplit
func Mfence()

//go:nosplit
func Fxsave(addr uintptr)
5 changes: 5 additions & 0 deletions sys/sys.s
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ TEXT ·Flags(SB), NOSPLIT, $0-4
TEXT ·Mfence(SB), NOSPLIT, $0
MFENCE
RET

TEXT ·Fxsave(SB), NOSPLIT, $0-4
MOVL addr+0(FP), AX
FXSAVE (AX)
RET

0 comments on commit a10ba64

Please sign in to comment.