github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/syscall/syscall_nacl.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syscall
     6  
     7  import (
     8  	"sync"
     9  	"unsafe"
    10  )
    11  
    12  //sys	naclClose(fd int) (err error) = sys_close
    13  //sys	Exit(code int) (err error)
    14  //sys	naclFstat(fd int, stat *Stat_t) (err error) = sys_fstat
    15  //sys	naclRead(fd int, b []byte) (n int, err error) = sys_read
    16  //sys	naclSeek(fd int, off *int64, whence int) (err error) = sys_lseek
    17  //sys	naclGetRandomBytes(b []byte) (err error) = sys_get_random_bytes
    18  
    19  const direntSize = 8 + 8 + 2 + 256
    20  
    21  // native_client/src/trusted/service_runtime/include/sys/dirent.h
    22  type Dirent struct {
    23  	Ino    int64
    24  	Off    int64
    25  	Reclen uint16
    26  	Name   [256]byte
    27  }
    28  
    29  func direntIno(buf []byte) (uint64, bool) {
    30  	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
    31  }
    32  
    33  func direntReclen(buf []byte) (uint64, bool) {
    34  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    35  }
    36  
    37  func direntNamlen(buf []byte) (uint64, bool) {
    38  	reclen, ok := direntReclen(buf)
    39  	if !ok {
    40  		return 0, false
    41  	}
    42  	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
    43  }
    44  
    45  const PathMax = 256
    46  
    47  // An Errno is an unsigned number describing an error condition.
    48  // It implements the error interface. The zero Errno is by convention
    49  // a non-error, so code to convert from Errno to error should use:
    50  //	err = nil
    51  //	if errno != 0 {
    52  //		err = errno
    53  //	}
    54  type Errno uintptr
    55  
    56  func (e Errno) Error() string {
    57  	if 0 <= int(e) && int(e) < len(errorstr) {
    58  		s := errorstr[e]
    59  		if s != "" {
    60  			return s
    61  		}
    62  	}
    63  	return "errno " + itoa(int(e))
    64  }
    65  
    66  func (e Errno) Temporary() bool {
    67  	return e == EINTR || e == EMFILE || e.Timeout()
    68  }
    69  
    70  func (e Errno) Timeout() bool {
    71  	return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
    72  }
    73  
    74  // A Signal is a number describing a process signal.
    75  // It implements the os.Signal interface.
    76  type Signal int
    77  
    78  const (
    79  	_ Signal = iota
    80  	SIGCHLD
    81  	SIGINT
    82  	SIGKILL
    83  	SIGTRAP
    84  	SIGQUIT
    85  )
    86  
    87  func (s Signal) Signal() {}
    88  
    89  func (s Signal) String() string {
    90  	if 0 <= s && int(s) < len(signals) {
    91  		str := signals[s]
    92  		if str != "" {
    93  			return str
    94  		}
    95  	}
    96  	return "signal " + itoa(int(s))
    97  }
    98  
    99  var signals = [...]string{}
   100  
   101  // File system
   102  
   103  const (
   104  	Stdin  = 0
   105  	Stdout = 1
   106  	Stderr = 2
   107  )
   108  
   109  // native_client/src/trusted/service_runtime/include/sys/fcntl.h
   110  const (
   111  	O_RDONLY  = 0
   112  	O_WRONLY  = 1
   113  	O_RDWR    = 2
   114  	O_ACCMODE = 3
   115  
   116  	O_CREAT    = 0100
   117  	O_CREATE   = O_CREAT // for ken
   118  	O_TRUNC    = 01000
   119  	O_APPEND   = 02000
   120  	O_EXCL     = 0200
   121  	O_NONBLOCK = 04000
   122  	O_NDELAY   = O_NONBLOCK
   123  	O_SYNC     = 010000
   124  	O_FSYNC    = O_SYNC
   125  	O_ASYNC    = 020000
   126  
   127  	O_CLOEXEC = 0
   128  
   129  	FD_CLOEXEC = 1
   130  )
   131  
   132  // native_client/src/trusted/service_runtime/include/sys/fcntl.h
   133  const (
   134  	F_DUPFD   = 0
   135  	F_GETFD   = 1
   136  	F_SETFD   = 2
   137  	F_GETFL   = 3
   138  	F_SETFL   = 4
   139  	F_GETOWN  = 5
   140  	F_SETOWN  = 6
   141  	F_GETLK   = 7
   142  	F_SETLK   = 8
   143  	F_SETLKW  = 9
   144  	F_RGETLK  = 10
   145  	F_RSETLK  = 11
   146  	F_CNVT    = 12
   147  	F_RSETLKW = 13
   148  
   149  	F_RDLCK   = 1
   150  	F_WRLCK   = 2
   151  	F_UNLCK   = 3
   152  	F_UNLKSYS = 4
   153  )
   154  
   155  // native_client/src/trusted/service_runtime/include/bits/stat.h
   156  const (
   157  	S_IFMT        = 0000370000
   158  	S_IFSHM_SYSV  = 0000300000
   159  	S_IFSEMA      = 0000270000
   160  	S_IFCOND      = 0000260000
   161  	S_IFMUTEX     = 0000250000
   162  	S_IFSHM       = 0000240000
   163  	S_IFBOUNDSOCK = 0000230000
   164  	S_IFSOCKADDR  = 0000220000
   165  	S_IFDSOCK     = 0000210000
   166  
   167  	S_IFSOCK = 0000140000
   168  	S_IFLNK  = 0000120000
   169  	S_IFREG  = 0000100000
   170  	S_IFBLK  = 0000060000
   171  	S_IFDIR  = 0000040000
   172  	S_IFCHR  = 0000020000
   173  	S_IFIFO  = 0000010000
   174  
   175  	S_UNSUP = 0000370000
   176  
   177  	S_ISUID = 0004000
   178  	S_ISGID = 0002000
   179  	S_ISVTX = 0001000
   180  
   181  	S_IREAD  = 0400
   182  	S_IWRITE = 0200
   183  	S_IEXEC  = 0100
   184  
   185  	S_IRWXU = 0700
   186  	S_IRUSR = 0400
   187  	S_IWUSR = 0200
   188  	S_IXUSR = 0100
   189  
   190  	S_IRWXG = 070
   191  	S_IRGRP = 040
   192  	S_IWGRP = 020
   193  	S_IXGRP = 010
   194  
   195  	S_IRWXO = 07
   196  	S_IROTH = 04
   197  	S_IWOTH = 02
   198  	S_IXOTH = 01
   199  )
   200  
   201  // native_client/src/trusted/service_runtime/include/sys/stat.h
   202  // native_client/src/trusted/service_runtime/include/machine/_types.h
   203  type Stat_t struct {
   204  	Dev       int64
   205  	Ino       uint64
   206  	Mode      uint32
   207  	Nlink     uint32
   208  	Uid       uint32
   209  	Gid       uint32
   210  	Rdev      int64
   211  	Size      int64
   212  	Blksize   int32
   213  	Blocks    int32
   214  	Atime     int64
   215  	AtimeNsec int64
   216  	Mtime     int64
   217  	MtimeNsec int64
   218  	Ctime     int64
   219  	CtimeNsec int64
   220  }
   221  
   222  // Processes
   223  // Not supported on NaCl - just enough for package os.
   224  
   225  var ForkLock sync.RWMutex
   226  
   227  type WaitStatus uint32
   228  
   229  func (w WaitStatus) Exited() bool       { return false }
   230  func (w WaitStatus) ExitStatus() int    { return 0 }
   231  func (w WaitStatus) Signaled() bool     { return false }
   232  func (w WaitStatus) Signal() Signal     { return 0 }
   233  func (w WaitStatus) CoreDump() bool     { return false }
   234  func (w WaitStatus) Stopped() bool      { return false }
   235  func (w WaitStatus) Continued() bool    { return false }
   236  func (w WaitStatus) StopSignal() Signal { return 0 }
   237  func (w WaitStatus) TrapCause() int     { return 0 }
   238  
   239  // XXX made up
   240  type Rusage struct {
   241  	Utime Timeval
   242  	Stime Timeval
   243  }
   244  
   245  // XXX made up
   246  type ProcAttr struct {
   247  	Dir   string
   248  	Env   []string
   249  	Files []uintptr
   250  	Sys   *SysProcAttr
   251  }
   252  
   253  type SysProcAttr struct {
   254  }
   255  
   256  // System
   257  
   258  func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   259  func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) { return 0, 0, ENOSYS }
   260  func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)           { return 0, 0, ENOSYS }
   261  func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
   262  	return 0, 0, ENOSYS
   263  }
   264  
   265  func Sysctl(key string) (string, error) {
   266  	if key == "kern.hostname" {
   267  		return "naclbox", nil
   268  	}
   269  	return "", ENOSYS
   270  }
   271  
   272  // Unimplemented Unix midden heap.
   273  
   274  const ImplementsGetwd = false
   275  
   276  func Getwd() (wd string, err error)     { return "", ENOSYS }
   277  func Getegid() int                      { return 1 }
   278  func Geteuid() int                      { return 1 }
   279  func Getgid() int                       { return 1 }
   280  func Getgroups() ([]int, error)         { return []int{1}, nil }
   281  func Getppid() int                      { return 2 }
   282  func Getpid() int                       { return 3 }
   283  func Gettimeofday(tv *Timeval) error    { return ENOSYS }
   284  func Getuid() int                       { return 1 }
   285  func Kill(pid int, signum Signal) error { return ENOSYS }
   286  func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   287  	return 0, ENOSYS
   288  }
   289  func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
   290  	return 0, 0, ENOSYS
   291  }
   292  func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   293  	return 0, ENOSYS
   294  }
   295  func RouteRIB(facility, param int) ([]byte, error)                { return nil, ENOSYS }
   296  func ParseRoutingMessage(b []byte) ([]RoutingMessage, error)      { return nil, ENOSYS }
   297  func ParseRoutingSockaddr(msg RoutingMessage) ([]Sockaddr, error) { return nil, ENOSYS }
   298  func SysctlUint32(name string) (value uint32, err error)          { return 0, ENOSYS }
   299  
   300  type Iovec struct{} // dummy