github.com/euank/go@v0.0.0-20160829210321-495514729181/src/runtime/os_nacl.go (about)

     1  // Copyright 2010 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 runtime
     6  
     7  import "unsafe"
     8  
     9  type mOS struct {
    10  	waitsema      int32 // semaphore for parking on locks
    11  	waitsemacount int32
    12  	waitsemalock  int32
    13  }
    14  
    15  func nacl_exception_stack(p uintptr, size int32) int32
    16  func nacl_exception_handler(fn uintptr, arg unsafe.Pointer) int32
    17  func nacl_sem_create(flag int32) int32
    18  func nacl_sem_wait(sem int32) int32
    19  func nacl_sem_post(sem int32) int32
    20  func nacl_mutex_create(flag int32) int32
    21  func nacl_mutex_lock(mutex int32) int32
    22  func nacl_mutex_trylock(mutex int32) int32
    23  func nacl_mutex_unlock(mutex int32) int32
    24  func nacl_cond_create(flag int32) int32
    25  func nacl_cond_wait(cond, n int32) int32
    26  func nacl_cond_signal(cond int32) int32
    27  func nacl_cond_broadcast(cond int32) int32
    28  
    29  //go:noescape
    30  func nacl_cond_timed_wait_abs(cond, lock int32, ts *timespec) int32
    31  func nacl_thread_create(fn uintptr, stk, tls, xx unsafe.Pointer) int32
    32  
    33  //go:noescape
    34  func nacl_nanosleep(ts, extra *timespec) int32
    35  func nanotime() int64
    36  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
    37  func exit(code int32)
    38  func osyield()
    39  
    40  //go:noescape
    41  func write(fd uintptr, p unsafe.Pointer, n int32) int32
    42  
    43  //go:linkname os_sigpipe os.sigpipe
    44  func os_sigpipe() {
    45  	throw("too many writes on closed pipe")
    46  }
    47  
    48  func dieFromSignal(sig int32) {
    49  	exit(2)
    50  }
    51  
    52  func sigpanic() {
    53  	g := getg()
    54  	if !canpanic(g) {
    55  		throw("unexpected signal during runtime execution")
    56  	}
    57  
    58  	// Native Client only invokes the exception handler for memory faults.
    59  	g.sig = _SIGSEGV
    60  	panicmem()
    61  }
    62  
    63  func raiseproc(sig int32) {
    64  }
    65  
    66  // Stubs so tests can link correctly. These should never be called.
    67  func open(name *byte, mode, perm int32) int32
    68  func closefd(fd int32) int32
    69  func read(fd int32, p unsafe.Pointer, n int32) int32
    70  
    71  type sigset struct{}
    72  
    73  // Called to initialize a new m (including the bootstrap m).
    74  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
    75  func mpreinit(mp *m) {
    76  	mp.gsignal = malg(32 * 1024)
    77  	mp.gsignal.m = mp
    78  }
    79  
    80  func sigtramp()
    81  
    82  //go:nosplit
    83  func msigsave(mp *m) {
    84  }
    85  
    86  //go:nosplit
    87  func msigrestore(sigmask sigset) {
    88  }
    89  
    90  //go:nosplit
    91  func sigblock() {
    92  }
    93  
    94  // Called to initialize a new m (including the bootstrap m).
    95  // Called on the new thread, cannot allocate memory.
    96  func minit() {
    97  	_g_ := getg()
    98  
    99  	// Initialize signal handling
   100  	ret := nacl_exception_stack(_g_.m.gsignal.stack.lo, 32*1024)
   101  	if ret < 0 {
   102  		print("runtime: nacl_exception_stack: error ", -ret, "\n")
   103  	}
   104  
   105  	ret = nacl_exception_handler(funcPC(sigtramp), nil)
   106  	if ret < 0 {
   107  		print("runtime: nacl_exception_handler: error ", -ret, "\n")
   108  	}
   109  }
   110  
   111  // Called from dropm to undo the effect of an minit.
   112  func unminit() {
   113  }
   114  
   115  func osinit() {
   116  	ncpu = 1
   117  	getg().m.procid = 2
   118  	//nacl_exception_handler(funcPC(sigtramp), nil);
   119  }
   120  
   121  func signame(sig uint32) string {
   122  	if sig >= uint32(len(sigtable)) {
   123  		return ""
   124  	}
   125  	return sigtable[sig].name
   126  }
   127  
   128  func crash() {
   129  	*(*int32)(nil) = 0
   130  }
   131  
   132  //go:noescape
   133  func getRandomData([]byte)
   134  
   135  func goenvs() {
   136  	goenvs_unix()
   137  }
   138  
   139  func initsig(preinit bool) {
   140  }
   141  
   142  //go:nosplit
   143  func usleep(us uint32) {
   144  	var ts timespec
   145  
   146  	ts.tv_sec = int64(us / 1e6)
   147  	ts.tv_nsec = int32(us%1e6) * 1e3
   148  	nacl_nanosleep(&ts, nil)
   149  }
   150  
   151  func mstart_nacl()
   152  
   153  // May run with m.p==nil, so write barriers are not allowed.
   154  //go:nowritebarrier
   155  func newosproc(mp *m, stk unsafe.Pointer) {
   156  	mp.tls[0] = uintptr(unsafe.Pointer(mp.g0))
   157  	mp.tls[1] = uintptr(unsafe.Pointer(mp))
   158  	ret := nacl_thread_create(funcPC(mstart_nacl), stk, unsafe.Pointer(&mp.tls[2]), nil)
   159  	if ret < 0 {
   160  		print("nacl_thread_create: error ", -ret, "\n")
   161  		throw("newosproc")
   162  	}
   163  }
   164  
   165  //go:nosplit
   166  func semacreate(mp *m) {
   167  	if mp.waitsema != 0 {
   168  		return
   169  	}
   170  	systemstack(func() {
   171  		mu := nacl_mutex_create(0)
   172  		if mu < 0 {
   173  			print("nacl_mutex_create: error ", -mu, "\n")
   174  			throw("semacreate")
   175  		}
   176  		c := nacl_cond_create(0)
   177  		if c < 0 {
   178  			print("nacl_cond_create: error ", -c, "\n")
   179  			throw("semacreate")
   180  		}
   181  		mp.waitsema = c
   182  		mp.waitsemalock = mu
   183  	})
   184  }
   185  
   186  //go:nosplit
   187  func semasleep(ns int64) int32 {
   188  	var ret int32
   189  
   190  	systemstack(func() {
   191  		_g_ := getg()
   192  		if nacl_mutex_lock(_g_.m.waitsemalock) < 0 {
   193  			throw("semasleep")
   194  		}
   195  
   196  		for _g_.m.waitsemacount == 0 {
   197  			if ns < 0 {
   198  				if nacl_cond_wait(_g_.m.waitsema, _g_.m.waitsemalock) < 0 {
   199  					throw("semasleep")
   200  				}
   201  			} else {
   202  				var ts timespec
   203  				end := ns + nanotime()
   204  				ts.tv_sec = end / 1e9
   205  				ts.tv_nsec = int32(end % 1e9)
   206  				r := nacl_cond_timed_wait_abs(_g_.m.waitsema, _g_.m.waitsemalock, &ts)
   207  				if r == -_ETIMEDOUT {
   208  					nacl_mutex_unlock(_g_.m.waitsemalock)
   209  					ret = -1
   210  					return
   211  				}
   212  				if r < 0 {
   213  					throw("semasleep")
   214  				}
   215  			}
   216  		}
   217  
   218  		_g_.m.waitsemacount = 0
   219  		nacl_mutex_unlock(_g_.m.waitsemalock)
   220  		ret = 0
   221  	})
   222  	return ret
   223  }
   224  
   225  //go:nosplit
   226  func semawakeup(mp *m) {
   227  	systemstack(func() {
   228  		if nacl_mutex_lock(mp.waitsemalock) < 0 {
   229  			throw("semawakeup")
   230  		}
   231  		if mp.waitsemacount != 0 {
   232  			throw("semawakeup")
   233  		}
   234  		mp.waitsemacount = 1
   235  		nacl_cond_signal(mp.waitsema)
   236  		nacl_mutex_unlock(mp.waitsemalock)
   237  	})
   238  }
   239  
   240  func memlimit() uintptr {
   241  	return 0
   242  }
   243  
   244  // This runs on a foreign stack, without an m or a g. No stack split.
   245  //go:nosplit
   246  //go:norace
   247  //go:nowritebarrierrec
   248  func badsignal(sig uintptr) {
   249  	cgocallback(unsafe.Pointer(funcPC(badsignalgo)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig), 0)
   250  }
   251  
   252  func badsignalgo(sig uintptr) {
   253  	if !sigsend(uint32(sig)) {
   254  		// A foreign thread received the signal sig, and the
   255  		// Go code does not want to handle it.
   256  		raisebadsignal(int32(sig))
   257  	}
   258  }
   259  
   260  // This runs on a foreign stack, without an m or a g. No stack split.
   261  //go:nosplit
   262  func badsignal2() {
   263  	write(2, unsafe.Pointer(&badsignal1[0]), int32(len(badsignal1)))
   264  	exit(2)
   265  }
   266  
   267  var badsignal1 = []byte("runtime: signal received on thread not created by Go.\n")
   268  
   269  func raisebadsignal(sig int32) {
   270  	badsignal2()
   271  }
   272  
   273  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {}
   274  func munmap(addr unsafe.Pointer, n uintptr)               {}
   275  func resetcpuprofiler(hz int32)                           {}
   276  func sigdisable(uint32)                                   {}
   277  func sigenable(uint32)                                    {}
   278  func sigignore(uint32)                                    {}
   279  func closeonexec(int32)                                   {}
   280  
   281  var writelock uint32 // test-and-set spin lock for write
   282  
   283  /*
   284  An attempt at IRT. Doesn't work. See end of sys_nacl_amd64.s.
   285  
   286  void (*nacl_irt_query)(void);
   287  
   288  int8 nacl_irt_basic_v0_1_str[] = "nacl-irt-basic-0.1";
   289  void *nacl_irt_basic_v0_1[6]; // exit, gettod, clock, nanosleep, sched_yield, sysconf
   290  int32 nacl_irt_basic_v0_1_size = sizeof(nacl_irt_basic_v0_1);
   291  
   292  int8 nacl_irt_memory_v0_3_str[] = "nacl-irt-memory-0.3";
   293  void *nacl_irt_memory_v0_3[3]; // mmap, munmap, mprotect
   294  int32 nacl_irt_memory_v0_3_size = sizeof(nacl_irt_memory_v0_3);
   295  
   296  int8 nacl_irt_thread_v0_1_str[] = "nacl-irt-thread-0.1";
   297  void *nacl_irt_thread_v0_1[3]; // thread_create, thread_exit, thread_nice
   298  int32 nacl_irt_thread_v0_1_size = sizeof(nacl_irt_thread_v0_1);
   299  */