Hello all - I've been having a hard time getting syscall.Mmap to produce 
the "right" values. I'm using 1.6.2 and cross-compiling for the ARM/Linux. 
I'm trying to read from Arm memory at its device address 0x43c00000. 

Here's the scenario: I 'poke' values into memory using the C based 
arm/linux 'poke' command:
/var/ftp# poke 0x43c00000 0x03020100
/var/ftp# poke 0x43c00004 0x07060504
/var/ftp# poke 0x43c00008 0x0b0a0908
/var/ftp# poke 0x43c0000c 0x0f0e0d0c

If I then 'read' from these memory locations with the C based peek command 
I get this (as expected):
/var/ftp# peek 0x43c00000
0x03020100
/var/ftp# peek 0x43c00004
0x07060504
/var/ftp# peek 0x43c00008
0x0b0a0908
/var/ftp# peek 0x43c0000c
0x0f0e0d0c

However, if I use a go based version to read this range, something is wrong 
(peek_orig.go, attached), results in:
/var/ftp# ./peekgo 0x43c00000
mem[0]:0x00     mem[1]:0x01     mem[2]:0x02     mem[3]:0x00     mem[4]:0x04 
    mem[5]:0x00     mem[6]:0x00     mem[7]:0x00
mem[8]:0x08     mem[9]:0x00     mem[10]:0x00    mem[11]:0x00   
 mem[12]:0x0c    mem[13]:0x00    mem[14]:0x00    mem[15]:0x00
mem[16]:0x00    mem[17]:0x00    mem[18]:0x00    mem[19]:0x00   
 mem[20]:0x00    mem[21]:0x00    mem[22]:0x00    mem[23]:0x00
mem[24]:0x00    mem[25]:0x00    mem[26]:0x00    mem[27]:0x00   
 mem[28]:0x00    mem[29]:0x00    mem[30]:0x00    mem[31]:0x00

I'll also attach the equivalent C based version of peek (peek.c).

I believe everything in the go version should work based on godoc -src 
syscall Mmap, which shows the actual call to the linux mmap function. The 
go version returns a []byte, but they're not the correct values.

Any help/suggestions would be very welcome!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: peek_orig.go
Description: Binary data

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

void usage(char *prog)
{
    printf("usage: %s ADDR\n",prog);
    printf("\n");
    printf("ADDR should be specified as hex values\n");
}


int main(int argc, char *argv[])
{
    int fd;
    void *ptr;
    unsigned addr, page_addr, page_offset;
    unsigned page_size=sysconf(_SC_PAGESIZE);

    if(argc!=2) {
        usage(argv[0]);
        exit(-1);
    }

    fd=open("/dev/mem",O_RDONLY);
    if(fd<1) {
        perror(argv[0]);
        exit(-1);
    }

    addr=strtoul(argv[1],NULL,0);
    page_addr=(addr & ~(page_size-1));
    page_offset=addr-page_addr;
    printf("addr: %08x, pagesize: %08x(%d), page_addr: %08x, offset: %08x\n", 
        addr, page_size, page_size, page_addr, page_offset ); 

    ptr=mmap(NULL,page_size,PROT_READ,MAP_SHARED,fd,page_addr);
    if((int)ptr==-1) {
        perror(argv[0]);
        exit(-1);
    }
    printf("0x%08x\n",*((unsigned *)(ptr+page_offset)));
    return 0;
}

Reply via email to