Implementing the msync system call in Go

375 views
Skip to first unread message

Lars Tørnes Hansen

unread,
Mar 21, 2016, 4:59:01 PM3/21/16
to golang-nuts
I need to update a mmap'ed /dev/gpiomem device file on a Raspberry Pi before I read, and after a GPIO hardware register was updated.

The msync syscall is unimplemented in the packages:

FYI,

1st question: Is the following correct?


In C, the msync syscall definition is:


int msync (void *, size_t, int);

http://git.musl-libc.org/cgit/musl/tree/include/sys/mman.h


The source code for that function can be found in:

http://git.musl-libc.org/cgit/musl/tree/src/mman/msync.c


... which contains this code:


#include <sys/mman.h>
#include "syscall.h"

int msync(void *start, size_t len, int flags)
{
    return syscall_cp(SYS_msync, start, len, flags);
}


 .. so I need to use:


func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)

... from package golang.org/x/sys/unix, where:

  • trap is unix.SYS_MSYNC
  • a1, a2, a3 is a *uint32 converted to a uintptr
  • a1 is the virtual memory base address from the bcm2835-gpiomem kernel space driver
  • a2 is the length (os.GetPagesize(), 4096 bytes) of the memory mapped /dev/gpiomem device file
  • a3 are the msync flags: unix.MS_SYNC && unix.MS_INVALIDATE, Device file is mmap'ed MM_SHARED

2nd question


The return value

err syscall.Errno
... is easy to grok, but what are the real type and meaning of the return values r1, and r2?

Thanks in advance for an answer..

Lars Tørnes Hansen

unread,
Mar 21, 2016, 5:04:00 PM3/21/16
to golang-nuts
Edit:
Not MM_SHARED, but MAP_SHARED

Ian Lance Taylor

unread,
Mar 21, 2016, 5:30:01 PM3/21/16
to Lars Tørnes Hansen, golang-nuts
On Mon, Mar 21, 2016 at 1:59 PM, Lars Tørnes Hansen <lar...@gmail.com> wrote:
>
> .. so I need to use:
>
>> func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
>
> ... from package golang.org/x/sys/unix, where:

You could also use the same Syscall function in the standard library
syscall package.

> trap is unix.SYS_MSYNC
> a1, a2, a3 is a *uint32 converted to a uintptr
> a1 is the virtual memory base address from the bcm2835-gpiomem kernel space
> driver
> a2 is the length (os.GetPagesize(), 4096 bytes) of the memory mapped
> /dev/gpiomem device file
> a3 are the msync flags: unix.MS_SYNC && unix.MS_INVALIDATE, Device file is
> mmap'ed MM_SHARED

Looks right.


> The return value
>>
>> err syscall.Errno
>
> ... is easy to grok, but what are the real type and meaning of the return
> values r1, and r2?

r1 is the return value of the system call; for msync, it will be 0 or
-1. r2 is, as far as I know, not used on GNU/Linux; it's there
because on some other systems it is used for system calls that return
two values, like pipe.

Ian
Reply all
Reply to author
Forward
0 new messages