Skip to content

Commit

Permalink
sync page cache after mmap (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
icexin authored Jul 16, 2021
1 parent 1d3ce5b commit 7809b5c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion e1000/e1000.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (d *driver) Init(dev *pci.Device) error {
if !ismem {
panic("not memory bar")
}
mm.Fixmap(uintptr(baddr), uintptr(baddr), uintptr(blen))
mm.SysFixedMmap(uintptr(baddr), uintptr(blen))
d.bar = uintptr(baddr)
debug.Logf("[e1000] mmap for bar0 0x%x", d.bar)

Expand Down
1 change: 1 addition & 0 deletions kernel/postinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/icexin/eggos/clock"
// called when go runtime init done
func Init() {
initClockTime()
idle_init()
go traploop()
go handleForward()
bootstrapDone = true
Expand Down
15 changes: 12 additions & 3 deletions kernel/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (

SYS_WAIT_IRQ = 500
SYS_WAIT_SYSCALL = 501
SYS_FIXED_MMAP = 502
)

const (
Expand All @@ -70,9 +71,9 @@ var (
SYS_EXIT, SYS_set_thread_area, SYS_sched_yield, SYS_nanosleep, SYS_brk,
SYS_munmap, SYS_mmap2, SYS_madvise, SYS_clone, SYS_gettid,
SYS_futex, SYS_rt_sigaction, SYS_rt_sigprocmask, SYS_sigaltstack,
SYS_clock_gettime, SYS_exit_group, SYS_WAIT_IRQ, SYS_WAIT_SYSCALL,
SYS_clock_gettime, SYS_exit_group,
syscall.SYS_EPOLL_CREATE1, syscall.SYS_EPOLL_CTL, syscall.SYS_EPOLL_WAIT,
// SYS_RANDOM,
SYS_WAIT_IRQ, SYS_WAIT_SYSCALL, SYS_FIXED_MMAP,
}

syscalltask threadptr
Expand Down Expand Up @@ -253,6 +254,8 @@ func doBootSyscall(no, a0, a1, a2, a3, a4, a5 uintptr) uintptr {
return waitIRQ()
case SYS_WAIT_SYSCALL:
return fetchPendingCall()
case SYS_FIXED_MMAP:
return fixedmmap(a0, a1)

default:
uart.WriteString("unknown syscall\n")
Expand All @@ -265,14 +268,20 @@ func doBootSyscall(no, a0, a1, a2, a3, a4, a5 uintptr) uintptr {
//go:nosplit
func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr {
// called on sysReserve
if prot == _PROT_NONE {
if prot == syscall.PROT_NONE {
return mm.Sbrk(n)
}

// called on sysMap and sysAlloc
return mm.Mmap(uintptr(addr), n)
}

//go:nopslit
func fixedmmap(addr uintptr, size uintptr) uintptr {
mm.Fixmap(addr, addr, size)
return addr
}

//go:nosplit
func syscal_init() {
epollInit()
Expand Down
13 changes: 8 additions & 5 deletions kernel/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,21 @@ func sys_hlt()
// thread0 is the first thread
//go:nosplit
func thread0() {
// jump to go rt0
go_entry()
panic("main return")
}

// run when after main init
func idle_init() {
// thread0 clone idle thread
stack := mm.Mmap(0, _THREAD_STACK_SIZE) + _THREAD_STACK_SIZE
stack := mm.SysMmap(0, _THREAD_STACK_SIZE) + _THREAD_STACK_SIZE
tid := sys_clone(sys.FuncPC(idle), stack)
idle_thread = (threadptr)(unsafe.Pointer(&threads[tid]))

// make idle thread running at ring0, so that it can call HLT instruction.
tf := idle_thread.ptr().tf
tf.CS = _KCODE_IDX << 3

// jump to go rt0
go_entry()
panic("main return")
}

//go:nosplit
Expand Down
4 changes: 4 additions & 0 deletions mm/mm.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,16 @@ func Mmap(va, size uintptr) uintptr {
va = kmm.sbrk(size)
}
vmm.mmap(va, size, PTE_P|PTE_W|PTE_U)
// flush page table cache
lcr3(vmm.pgdir)
return va
}

//go:nosplit
func Fixmap(va, pa, size uintptr) {
vmm.fixmap(va, pa, size, PTE_P|PTE_W|PTE_U)
// flush page table cache
lcr3(vmm.pgdir)
}

//go:nosplit
Expand Down
29 changes: 29 additions & 0 deletions mm/sys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mm

import (
"syscall"
)

const (
// sync with kernel
_SYS_FIXED_MMAP = 502
)

// SysMmap like Mmap but can run in user mode
// wraper of syscall.Mmap
func SysMmap(vaddr, size uintptr) uintptr {
mem, _, err := syscall.Syscall6(syscall.SYS_MMAP2, uintptr(vaddr), size, syscall.PROT_READ|syscall.PROT_WRITE, 0, 0, 0)
if err != 0 {
panic(err.Error())
}
return mem
}

// SysFixedMmap map the same physical address to the virtual address
// run in user mode
func SysFixedMmap(addr, size uintptr) {
_, _, err := syscall.Syscall(_SYS_FIXED_MMAP, addr, size, 0)
if err != 0 {
panic(err.Error())
}
}
2 changes: 1 addition & 1 deletion vbe/fb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Init() {
Pitch: bootInfo.FramebufferPitch,
}

mm.Fixmap(uintptr(info.Addr), uintptr(info.Addr), uintptr(info.Width*info.Height*4))
mm.SysFixedMmap(uintptr(info.Addr), uintptr(info.Width*info.Height*4))
fbbuf = (*[10 << 20]uint8)(unsafe.Pointer(uintptr(info.Addr)))[:info.Width*info.Height*4]
buffer = make([]uint8, len(fbbuf))
DefaultView = NewView()
Expand Down

0 comments on commit 7809b5c

Please sign in to comment.