func newMap(f *os.File, opts ...Option) (*mmap, error) { m := &mmap{ flags: -1, prot: -1, len: -1, } for _, opt := range opts { opt(m) } if m.flags == -1 || m.prot == -1 { return nil, fmt.Errorf("must pass options to set the flag or prot values") } if f == nil && !m.anon { return nil, fmt.Errorf("f arg cannot be nil, anon was %v", m.anon) } var fd uintptr if m.anon { fd = ^uintptr(0) m.flags = m.flags | syscall.MAP_ANON if m.len <= 0 { return nil, fmt.Errorf("must use Length() if using Anon() option") } } else { fd = f.Fd() s, err := f.Stat() if err != nil { return nil, err } if s.Size() == 0 { return nil, fmt.Errorf("cannot mmap 0 length file") } if m.len == -1 { m.len = int(s.Size()) } } var err error if m.anon { m.data, err = syscall.Mmap(-1, 0, m.len, m.prot, m.flags) } else { m.data, err = syscall.Mmap(int(fd), m.offset, m.len, m.prot, m.flags) } if err != nil { return nil, fmt.Errorf("problem with mmap system call: %q", err) } return m, nil }
// Open returns a pisearch object that references the two files // name.4.idx and name.4.bin, or error if the files could not // be opened and memory mapped. func Open(name string) (pisearch *Pisearch, err error) { file, err := os.Open(name + ".4.bin") if err != nil { log.Println("open of .4.bin failed") return nil, err } fi, err := file.Stat() if err != nil { log.Println("stat failed") file.Close() return nil, err } numdigits := fi.Size() * 2 idxfile, err := os.Open(name + ".4.idx") if err != nil { log.Println("open of .4.idx failed") file.Close() return nil, err } idxfi, err := idxfile.Stat() if err != nil { log.Println("stat of idx failed") idxfile.Close() file.Close() return nil, err } filemap, err := syscall.Mmap(int(file.Fd()), 0, int(fi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE) if err != nil { log.Println("mmap of file failed") file.Close() idxfile.Close() return nil, err } idxmap, err := syscall.Mmap(int(idxfile.Fd()), 0, int(idxfi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE) if err != nil { log.Println("mmap of idx file failed") syscall.Munmap(filemap) file.Close() idxfile.Close() return nil, err } return &Pisearch{file, filemap, int(numdigits), idxfile, idxmap}, nil }
// seqMmapRead does n sequential reads of records of size rsize using mmap and copy. func seqMmapRead(fname string, rsize int, n int) (int, error) { f, err := os.Open(fname) if err != nil { return 0, err } defer f.Close() fi, err := f.Stat() if err != nil { return 0, err } fsize := int(fi.Size()) data, err := syscall.Mmap(int(f.Fd()), 0, fsize, syscall.PROT_READ, syscall.MAP_FILE|syscall.MAP_SHARED) // syscall.Madvise(data, syscall.MADV_SEQUENTIAL) defer syscall.Munmap(data) buf := make([]byte, rsize) if n <= 0 || n > fsize/rsize { n = fsize / rsize } var i int for i = 0; i < n; i++ { offset := i * rsize copy(buf, data[offset:offset+len(buf)]) } return i, nil }
func newInputPreloader(filePath string) (*inputPreloader, error) { if filePath == "/dev/null" { return nil, nil } file, err := os.Open(filePath) if err != nil { return nil, err } info, err := file.Stat() if err != nil { return nil, err } preloader := &inputPreloader{ file: file, fileSize: info.Size(), } mapping, err := syscall.Mmap( int(preloader.file.Fd()), 0, int(preloader.fileSize), syscall.PROT_READ, syscall.MAP_SHARED, ) if err == nil { pageSize := os.Getpagesize() preloader.mapping = mapping for i := 0; i < int(preloader.fileSize); i += pageSize { preloader.checksum += preloader.mapping[i] } } else { // mmap failed, so just read all the file. io.Copy(ioutil.Discard, preloader.file) } return preloader, nil }
func (this *ByteProfile) FindValue(doc_id int64) ([]byte, error) { if doc_id >= this.Len || doc_id < 1 { return nil, errors.New("docid is wrong") } if this.ProfileList[doc_id].InMomory == true { return this.ProfileList[doc_id].Data, nil } f, _ := os.Open(fmt.Sprintf("./index/%v_pfl.dat", this.Name)) defer f.Close() fi, err := f.Stat() if err != nil { fmt.Printf("ERR:%v", err) } MmapBytes, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE) if err != nil { fmt.Printf("MAPPING ERROR %v \n", err) return nil, err } defer syscall.Munmap(MmapBytes) StartPos := int(this.ProfileList[doc_id].Start) EndPos := this.ProfileList[doc_id].DataLen + StartPos this.ProfileList[doc_id].Data = make([]byte, this.ProfileList[doc_id].DataLen) copy(this.ProfileList[doc_id].Data, MmapBytes[StartPos:EndPos]) //fmt.Printf("Cost Time : %v \n",functime("MmapBytes")) this.ProfileList[doc_id].InMomory = true //fmt.Printf("list : %v\n", string(this.ProfileList[doc_id].Data)) return this.ProfileList[doc_id].Data, nil }
func NewCGroup(pid int) (*Cgroup, error) { fn := filepath.Join(Dir(pid), "cgroup") f, err := os.Open(fn) if err != nil { return nil, errors.New(fmt.Sprintf("Failed to read %s [%s]", fn, err)) } defer f.Close() stat, err := f.Stat() if err != nil { return nil, errors.New(fmt.Sprintf("Failed to stat %s [%s]", fn, err)) } b, err := syscall.Mmap(int(f.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE) if err != nil { return nil, errors.New(fmt.Sprintf("Failed to mmap %s [%s]", fn, err)) } defer syscall.Munmap(b) return NewCgroupFromReader(bytes.NewReader(b)), nil }
func helperMmap() { f, err := os.Create("child") if err != nil { log.Fatalf("Create: %v", err) } defer f.Close() data, err := syscall.Mmap(int(f.Fd()), 0, mmapSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { log.Fatalf("Mmap: %v", err) } for i, b := range mmapWrites { data[i] = b } if err := syscallx.Msync(data, syscall.MS_SYNC); err != nil { log.Fatalf("Msync: %v", err) } if err := syscall.Munmap(data); err != nil { log.Fatalf("Munmap: %v", err) } if err := f.Sync(); err != nil { log.Fatalf("Fsync = %v", err) } err = f.Close() if err != nil { log.Fatalf("Close: %v", err) } }
func (t *FsyncErrorTest) Msync() { var err error // On OS X, msync does not cause SyncFile. if isDarwin { return } // Open the file. t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0) AssertEq(nil, err) // mmap the file. data, err := syscall.Mmap( int(t.f1.Fd()), 0, 4, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) AssertEq(nil, err) defer syscall.Munmap(data) // msync the mapping. err = msync(data) ExpectThat(err, Error(HasSubstr("no such file"))) // Unmap. err = syscall.Munmap(data) AssertEq(nil, err) }
func initGPIO(memfd int) { buf, err := syscall.Mmap(memfd, BCM2835_GPIO_BASE, BCM2835_BLOCK_SIZE, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { log.Fatalf("rpi: unable to mmap GPIO page: %v", err) } gpfsel = []*uint32{ (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL0])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL1])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL2])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL3])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL4])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL5])), } gpset = []*uint32{ (*uint32)(unsafe.Pointer(&buf[BCM2835_GPSET0])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPSET1])), } gpclr = []*uint32{ (*uint32)(unsafe.Pointer(&buf[BCM2835_GPCLR0])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPCLR1])), } gplev = []*uint32{ (*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV0])), (*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV1])), } }
// Open takes a string path to a MaxMind DB file and returns a Reader // structure or an error. The database file is opened using a memory map. Use // the Close method on the Reader object to return the resources to the // system. func Open(file string) (*Reader, error) { mapFile, err := os.Open(file) if err != nil { return nil, err } stats, err := mapFile.Stat() if err != nil { return nil, err } fileSize := int(stats.Size()) mmap, err := syscall.Mmap(int(mapFile.Fd()), 0, fileSize, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { return nil, err } reader, err := FromBytes(mmap) if err != nil { syscall.Munmap(mmap) mapFile.Close() return nil, err } reader.file = mapFile return reader, nil }
func NodeStoreWatch(neopath string, ch chan string) { file, err := os.Open(neopath + "/neostore.nodestore.db") fd := int(file.Fd()) mmap, err := syscall.Mmap(fd, 0, 100*NODE_SIZE, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { fmt.Println(err) os.Exit(1) } node := NodeRec{} mmap_slice := make([]byte, len(mmap)) copy(mmap_slice, mmap) go func() { for { for i := 0; i < 100; i++ { if !bytes.Equal(mmap[i*NODE_SIZE:i*NODE_SIZE+NODE_SIZE], mmap_slice[i*NODE_SIZE:i*NODE_SIZE+NODE_SIZE]) { node.Read(uint64(i), mmap) fmt.Printf("updated node idx %d: \n", i) fmt.Println(node) } } copy(mmap_slice, mmap) time.Sleep(time.Millisecond * time.Duration(rand.Int()%100)) } }() }
func main() { const n = 4096 * 50 // size of shared memory t := int(1) * n fmt.Println("Total size of shared memory : ", t) // open device file mmap_file, err := os.OpenFile(device_file, os.O_RDWR, 0666) if err != nil { fmt.Println(err) os.Exit(1) } defer mmap_file.Close() // mmap memory mmap, err := syscall.Mmap(int(mmap_file.Fd()), 0, int(t), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { fmt.Println(err) os.Exit(1) } defer syscall.Munmap(mmap) mmap_array := (*[n]byte)(unsafe.Pointer(&mmap[0])) fmt.Println(*mmap_array) // using memcpy to copy mmap memory buffer into program buffer mmap_buf := make([]byte, t) copy(mmap_buf, mmap_array[:]) fmt.Println(mmap_buf) }
func makeMemory(size int, extraProt, extraFlags int) (mem []byte, err error) { if size == 0 { return } return syscall.Mmap(-1, 0, size, syscall.PROT_READ|syscall.PROT_WRITE|extraProt, syscall.MAP_PRIVATE|syscall.MAP_ANONYMOUS|extraFlags) }
// mmap memory maps a DB's data file. func mmap(db *DB, sz int) error { // Truncate and fsync to ensure file size metadata is flushed. // https://github.com/boltdb/bolt/issues/284 if !db.NoGrowSync && !db.readOnly { if err := db.file.Truncate(int64(sz)); err != nil { return fmt.Errorf("file resize error: %s", err) } if err := db.file.Sync(); err != nil { return fmt.Errorf("file sync error: %s", err) } } // Map the data file to memory. b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { return err } // Advise the kernel that the mmap is accessed randomly. if err := madvise(b, syscall.MADV_RANDOM); err != nil { return fmt.Errorf("madvise: %s", err) } // Save the original byte slice and convert to a byte array pointer. db.dataref = b db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) db.datasz = sz return nil }
// NewBufferMemoryFs maps a mounted file system into shared memory and returns a handle to the shared memory buffer func NewBufferMemoryFs(dir string, size, prot int) (Buffer, error) { files, err := createBuffer(dir, size) if err != nil { return nil, err } file, localFile := files[0], files[1] // MAP_NORESERVE is specifically not used because it allows mmap // to succeed even when there are no huge pages reserved. const flags = syscall.MAP_SHARED | syscall.MAP_POPULATE b, err := syscall.Mmap(int(file.Fd()), 0, size, prot, flags) if err != nil { checkClose(file) checkClose(localFile) return nil, os.NewSyscallError("NewBufferMemoryFs: mmap", err) } buf := &sharedBuffer{file, localFile, b, stackToKeep()} // Lock everything to avoid SIGBUS when trying to use memory // mapped on a tmpfs that becomes full after the Statfs check in // createBuffer. if err := buf.LockAll(); err != nil { checkClose(buf) return nil, err } runtime.SetFinalizer(buf, (*sharedBuffer).finalize) return buf, nil }
func openRegion(f *os.File, writeable bool, offset int64) (*region, error) { _, err := f.Seek(offset, os.SEEK_SET) if err != nil { return nil, err } header := make([]byte, regionHeaderSize) _, err = f.Read(header) if err != nil { return nil, err } if bytes.Compare(header[:8], signature) != 0 { return nil, InvalidSignature } size := *(*int64)(unsafe.Pointer(&header[regionSizeOffset])) rid := *(*int64)(unsafe.Pointer(&header[regionId])) flags := syscall.PROT_READ if writeable { flags |= syscall.PROT_WRITE } d, err := syscall.Mmap(int(f.Fd()), offset, int(size), flags, syscall.MAP_SHARED) if err != nil { return nil, err } r := ®ion{ id: rid, f: f, d: d, freePtr: d[regionFreePointerOffset : regionFreePointerOffset+8], blockListNextIdPtr: d[len(d)-8 : len(d)], } return r, nil }
// NewBufferFile maps a file to shared memory and returns a handle to the shared memory buffer func NewBufferFile(file *os.File, size, prot int) (Buffer, error) { fi, err := file.Stat() if err != nil { return nil, err } sys := fi.Sys().(*syscall.Stat_t) if sys.Size != int64(size) { return nil, errWrongSize } // Dup to allow file parameter to be closed regardless fd, err := syscall.Dup(int(file.Fd())) if err != nil { return nil, err } const flags = syscall.MAP_SHARED b, err := syscall.Mmap(fd, 0, size, prot, flags) if err != nil { return nil, err } // localFile is nil because fd is from somewhere else buf := &sharedBuffer{os.NewFile(uintptr(fd), ""), nil, b, stackToKeep()} runtime.SetFinalizer(buf, (*sharedBuffer).finalize) return buf, nil }
// reload reads back info about b from b.filename. // b.filename should be set. func (b *Buffer) reload() error { stat, err := os.Stat(b.filename) if err != nil { return err } fsize := uint64(stat.Size()) b.capacity = fsize - metadata f, err := os.OpenFile(b.filename, os.O_RDWR, 0600) if err != nil { return err } data, err := syscall.Mmap( int(f.Fd()), 0, int(fsize), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED, ) if err != nil { return err } b.data = data off := int(b.capacity) b.first = binary.GetLittleEndianUint64(b.data, off) b.last = binary.GetLittleEndianUint64(b.data, off+8) b.nextSeq = binary.GetLittleEndianUint64(b.data, off+16) b.biggest = binary.GetLittleEndianUint32(b.data, off+24) b.length = binary.GetLittleEndianUint64(b.data, off+28) return nil }
func NewS4548(path string) *S4548 { screen := new(S4548) screen.path = path var err error screen.fp = new(os.File) screen.fp, err = os.OpenFile(path, os.O_RDWR, os.ModeDevice|os.ModeCharDevice) if err != nil { panic(err) } //screen.fb = make([]uint8, WIDTH*(HEIGHT / 8)) screen.fb, err = syscall.Mmap(int(screen.fp.Fd()), 0, WIDTH*(HEIGHT/8), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { panic(err) } p := color.Palette{color.White, color.Black} r := image.Rect(0, 0, WIDTH, HEIGHT) screen.Paletted = image.NewPaletted(r, p) return screen }
func MMapPal(filename string) (*Pal, error) { p := &Pal{} fi, err := os.Stat(filename) if err != nil { return nil, err } if fi.IsDir() { return nil, errors.New(fmt.Sprintf("%s is a directory, file needed", filename)) } file, err := os.OpenFile(filename, os.O_RDWR, 0777) if err != nil { return nil, err } defer file.Close() mmap, err := syscall.Mmap(int(file.Fd()), 0, int(fi.Size()), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { return nil, err } err = p.From(mmap) if err != nil { syscall.Munmap(mmap) return nil, err } p.mmaped = true runtime.SetFinalizer(p, func(p *Pal) { p.Free() }) return p, nil }
func New(capacity int, filename string) (*Buffer, error) { if _, err := os.Stat(filename); !os.IsNotExist(err) { return nil, ErrFileExists } f, err := os.Create(filename) if err != nil { return nil, err } defer f.Close() fsize := capacity + metadata if err := syscall.Fallocate(int(f.Fd()), 0, 0, int64(fsize)); err != nil { return nil, err } data, err := syscall.Mmap( int(f.Fd()), 0, fsize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED, ) // TODO(ashish): Call msync periodically. if err != nil { return nil, err } b := &Buffer{ capacity: uint64(capacity), // since it's used with uint64s a lot more filename: f.Name(), data: data, } b.updateMeta() return b, nil }
func ReadLastNLines(f *os.File, lines int) (string, error) { if lines <= 0 { return "", fmt.Errorf("invalid line count") } stat, err := f.Stat() if err != nil { return "", err } data, err := syscall.Mmap(int(f.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { return "", err } defer syscall.Munmap(data) for i := len(data) - 1; i >= 0; i-- { if data[i] == '\n' { lines-- } if lines < 0 { return string(data[i+1 : len(data)]), nil } } return string(data), nil }
func main() { // Turn the eventual fault into a panic, not a program crash, // so that memcopy can recover. debug.SetPanicOnFault(true) size := syscall.Getpagesize() // Map 16 pages of data with a 4-page hole in the middle. data, err := syscall.Mmap(-1, 0, 16*size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE) if err != nil { log.Fatalf("mmap: %v", err) } // Note: Cannot call syscall.Munmap, because Munmap checks // that you are unmapping a whole region returned by Mmap. // We are trying to unmap just a hole in the middle. if _, _, err := syscall.Syscall(syscall.SYS_MUNMAP, uintptr(unsafe.Pointer(&data[8*size])), uintptr(4*size), 0); err != 0 { log.Fatalf("munmap: %v", err) } other := make([]byte, 16*size) // Check that memcopy returns the actual amount copied // before the fault (8*size - 5, the offset we skip in the argument). n, err := memcopy(data[5:], other) if err == nil { log.Fatal("no error from memcopy across memory hole") } if n != 8*size-5 { log.Fatal("memcopy returned %d, want %d", n, 8*size-5) } }
func (mc *mmapCache) MapFile(filename string, count int) (*mmapData, error) { mc.Lock() defer mc.Unlock() // File is already open and mmapped md, ok := mc.files[filename] if ok { return md, nil } // Open the file file, err := os.Open(filename) if err != nil { return nil, err } // Stat the file st, err := file.Stat() if err != nil { return nil, err } size := int(st.Size()) // Mmap the file data, err := syscall.Mmap(int(file.Fd()), 0, (size+4095) & ^4095, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { return nil, err } // Cache the information and return the mmap mc.files[filename] = &mmapData{file: file, data: data, count: count} return mc.files[filename], nil }
func initWrite() []node { // prepare const ( openFlags = os.O_RDWR mmapProts = syscall.PROT_READ | syscall.PROT_WRITE ) // open file file, err := os.OpenFile(filePath, openFlags, 0) if err != nil { panic(fmt.Sprintf("cannot find file '%v' for package 'attr': %v", filePath, err)) } // get file size ls, err := file.Stat() if err != nil { file.Close() panic(fmt.Sprintf("cannot lstat file '%v' for package 'attr': %v", filePath, err)) } size := ls.Size() // calc node count len := size / int64(unsafe.Sizeof(node{})) if len > maxLen { len = maxLen } // memory map ptr, err := syscall.Mmap(int(file.Fd()), 0, int(size), mmapProts, syscall.MAP_SHARED) if err != nil { panic(fmt.Sprintf("cannot mmap file '%v'(size=%v) for package 'attr': %v", filePath, size, err)) } return (*[maxLen]node)(unsafe.Pointer(&ptr[0]))[:len] }
// create a new zim reader func NewReader(path string, mmap bool) (*ZimReader, error) { f, err := os.Open(path) if err != nil { return nil, err } z := ZimReader{f: f, mainPage: 0xffffffff, layoutPage: 0xffffffff} fi, err := f.Stat() if err != nil { return nil, err } size := fi.Size() if mmap { // we need a multiple of page size bigger than the file pc := size / int64(os.Getpagesize()) totalMmap := pc*int64(os.Getpagesize()) + int64(os.Getpagesize()) if (size % int64(os.Getpagesize())) == 0 { totalMmap = size } mmap, err := syscall.Mmap(int(f.Fd()), 0, int(totalMmap), syscall.PROT_READ, syscall.MAP_PRIVATE) if err != nil { return nil, err } z.mmap = mmap } err = z.readFileHeaders() return &z, err }
// Ensure the file ahs room for more data. func (file *File) Ensure(more uint64) (err error) { if file.Append+more <= file.Size { return } if file.Buf != nil { if err = syscall.Munmap(file.Buf); err != nil { return } } if _, err = file.Fh.Seek(0, os.SEEK_END); err != nil { return } if _, err = file.Fh.Write(make([]byte, file.Growth)); err != nil { return } if err = file.Fh.Sync(); err != nil { return } if newSize := int(file.Size + file.Growth); newSize < 0 { log.Panicf("File %s is getting too large", file.Name) } else if file.Buf, err = syscall.Mmap(int(file.Fh.Fd()), 0, newSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED); err != nil { return } file.Size += file.Growth log.Printf("File %s has grown %d bytes\n", file.Name, file.Growth) return file.Ensure(more) }
func createMapping(size int) (f *os.File, mem []byte, err error) { f, err = ioutil.TempFile("./", "syzkaller-shm") if err != nil { err = fmt.Errorf("failed to create temp file: %v", err) return } if err = f.Truncate(int64(size)); err != nil { err = fmt.Errorf("failed to truncate shm file: %v", err) f.Close() os.Remove(f.Name()) return } f.Close() f, err = os.OpenFile(f.Name(), os.O_RDWR, 0) if err != nil { err = fmt.Errorf("failed to open shm file: %v", err) os.Remove(f.Name()) return } mem, err = syscall.Mmap(int(f.Fd()), 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) if err != nil { err = fmt.Errorf("failed to mmap shm file: %v", err) f.Close() os.Remove(f.Name()) return } return }
func main() { var width int var value int64 // // check parameters // argc := len(os.Args) if argc < 3 || argc > 4 { usage() os.Exit(1) } switch os.Args[2] { case "b": width = size8bit case "h": width = size16bit case "w": width = size32bit case "d": width = size64bit default: usage() os.Exit(1) } address := atoInt(os.Args[1]) if argc == 4 { value = atoInt(os.Args[3]) } file, err := os.OpenFile("/dev/mem", fileProt, 0644) if err != nil { log.Fatal(err) } defer file.Close() fd := int(file.Fd()) base := address &^ pageSizeMask offset := address & pageSizeMask ptr, err := syscall.Mmap(fd, base, pageSize, mapProt, mapFlag) if err != nil { log.Fatal(err) } if argc == 4 { writeMem(ptr, width, offset, value) } addr, data := readMem(ptr, width, offset) fmt.Printf("0x%x 0x%x\n", addr+base, data) }
func mmapFile(f *os.File) ([]byte, error) { stat, err := f.Stat() if err != nil { return nil, err } return syscall.Mmap(int(f.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE) }