Last active
July 12, 2022 22:57
-
-
Save tetsu-koba/91e7f621c89392ca4d763413433c2b9c to your computer and use it in GitHub Desktop.
Using UIO from golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Using UIO from golang |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "math" | |
| "os" | |
| "syscall" | |
| "unsafe" | |
| ) | |
| func main() { | |
| mem := mmapRegs32(0, 0x200) | |
| for i, v := range mem { | |
| fmt.Printf("%08x: %08x\n", i*4, v) | |
| } | |
| } | |
| func mmapRegs32(addr uint32, n int) []uint32 { | |
| file := "/dev/uio0" | |
| f, err := os.Open(file) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| offset := int64(addr) &^ 0xfff | |
| data, err := syscall.Mmap(int(f.Fd()), offset, (n+0xfff)&^0xfff, syscall.PROT_READ, syscall.MAP_SHARED) | |
| if err != nil { | |
| log.Fatalf("mmap %s: %v", f.Name(), err) | |
| } | |
| f.Close() | |
| map_array := (*[math.MaxInt32 / 4]uint32)(unsafe.Pointer(&data[0])) | |
| return map_array[(addr&0xfff)/4 : ((int(addr)&0xfff)+n)/4] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment