github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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 uint32) {
    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 uint32) {
    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  //go:nowritebarrierrec
    92  func clearSignalHandlers() {
    93  }
    94  
    95  //go:nosplit
    96  func sigblock() {
    97  }
    98  
    99  // Called to initialize a new m (including the bootstrap m).
   100  // Called on the new thread, cannot allocate memory.
   101  func minit() {
   102  	_g_ := getg()
   103  
   104  	// Initialize signal handling
   105  	ret := nacl_exception_stack(_g_.m.gsignal.stack.lo, 32*1024)
   106  	if ret < 0 {
   107  		print("runtime: nacl_exception_stack: error ", -ret, "\n")
   108  	}
   109  
   110  	ret = nacl_exception_handler(funcPC(sigtramp), nil)
   111  	if ret < 0 {
   112  		print("runtime: nacl_exception_handler: error ", -ret, "\n")
   113  	}
   114  }
   115  
   116  // Called from dropm to undo the effect of an minit.
   117  func unminit() {
   118  }
   119  
   120  func osinit() {
   121  	ncpu = 1
   122  	getg().m.procid = 2
   123  	//nacl_exception_handler(funcPC(sigtramp), nil);
   124  	physPageSize = 65536
   125  }
   126  
   127  func signame(sig uint32) string {
   128  	if sig >= uint32(len(sigtable)) {
   129  		return ""
   130  	}
   131  	return sigtable[sig].name
   132  }
   133  
   134  func crash() {
   135  	*(*int32)(nil) = 0
   136  }
   137  
   138  //go:noescape
   139  func getRandomData([]byte)
   140  
   141  func goenvs() {
   142  	goenvs_unix()
   143  }
   144  
   145  func initsig(preinit bool) {
   146  }
   147  
   148  //go:nosplit
   149  func usleep(us uint32) {
   150  	var ts timespec
   151  
   152  	ts.tv_sec = int64(us / 1e6)
   153  	ts.tv_nsec = int32(us%1e6) * 1e3
   154  	nacl_nanosleep(&ts, nil)
   155  }
   156  
   157  func mstart_nacl()
   158  
   159  // May run with m.p==nil, so write barriers are not allowed.
   160  //go:nowritebarrier
   161  func newosproc(mp *m, stk unsafe.Pointer) {
   162  	mp.tls[0] = uintptr(unsafe.Pointer(mp.g0))
   163  	mp.tls[1] = uintptr(unsafe.Pointer(mp))
   164  	ret := nacl_thread_create(funcPC(mstart_nacl), stk, unsafe.Pointer(&mp.tls[2]), nil)
   165  	if ret < 0 {
   166  		print("nacl_thread_create: error ", -ret, "\n")
   167  		throw("newosproc")
   168  	}
   169  }
   170  
   171  //go:nosplit
   172  func semacreate(mp *m) {
   173  	if mp.waitsema != 0 {
   174  		return
   175  	}
   176  	systemstack(func() {
   177  		mu := nacl_mutex_create(0)
   178  		if mu < 0 {
   179  			print("nacl_mutex_create: error ", -mu, "\n")
   180  			throw("semacreate")
   181  		}
   182  		c := nacl_cond_create(0)
   183  		if c < 0 {
   184  			print("nacl_cond_create: error ", -c, "\n")
   185  			throw("semacreate")
   186  		}
   187  		mp.waitsema = c
   188  		mp.waitsemalock = mu
   189  	})
   190  }
   191  
   192  //go:nosplit
   193  func semasleep(ns int64) int32 {
   194  	var ret int32
   195  
   196  	systemstack(func() {
   197  		_g_ := getg()
   198  		if nacl_mutex_lock(_g_.m.waitsemalock) < 0 {
   199  			throw("semasleep")
   200  		}
   201  
   202  		for _g_.m.waitsemacount == 0 {
   203  			if ns < 0 {
   204  				if nacl_cond_wait(_g_.m.waitsema, _g_.m.waitsemalock) < 0 {
   205  					throw("semasleep")
   206  				}
   207  			} else {
   208  				var ts timespec
   209  				end := ns + nanotime()
   210  				ts.tv_sec = end / 1e9
   211  				ts.tv_nsec = int32(end % 1e9)
   212  				r := nacl_cond_timed_wait_abs(_g_.m.waitsema, _g_.m.waitsemalock, &ts)
   213  				if r == -_ETIMEDOUT {
   214  					nacl_mutex_unlock(_g_.m.waitsemalock)
   215  					ret = -1
   216  					return
   217  				}
   218  				if r < 0 {
   219  					throw("semasleep")
   220  				}
   221  			}
   222  		}
   223  
   224  		_g_.m.waitsemacount = 0
   225  		nacl_mutex_unlock(_g_.m.waitsemalock)
   226  		ret = 0
   227  	})
   228  	return ret
   229  }
   230  
   231  //go:nosplit
   232  func semawakeup(mp *m) {
   233  	systemstack(func() {
   234  		if nacl_mutex_lock(mp.waitsemalock) < 0 {
   235  			throw("semawakeup")
   236  		}
   237  		if mp.waitsemacount != 0 {
   238  			throw("semawakeup")
   239  		}
   240  		mp.waitsemacount = 1
   241  		nacl_cond_signal(mp.waitsema)
   242  		nacl_mutex_unlock(mp.waitsemalock)
   243  	})
   244  }
   245  
   246  func memlimit() uintptr {
   247  	return 0
   248  }
   249  
   250  // This runs on a foreign stack, without an m or a g. No stack split.
   251  //go:nosplit
   252  //go:norace
   253  //go:nowritebarrierrec
   254  func badsignal(sig uintptr) {
   255  	cgocallback(unsafe.Pointer(funcPC(badsignalgo)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig), 0)
   256  }
   257  
   258  func badsignalgo(sig uintptr) {
   259  	if !sigsend(uint32(sig)) {
   260  		// A foreign thread received the signal sig, and the
   261  		// Go code does not want to handle it.
   262  		raisebadsignal(uint32(sig))
   263  	}
   264  }
   265  
   266  // This runs on a foreign stack, without an m or a g. No stack split.
   267  //go:nosplit
   268  func badsignal2() {
   269  	write(2, unsafe.Pointer(&badsignal1[0]), int32(len(badsignal1)))
   270  	exit(2)
   271  }
   272  
   273  var badsignal1 = []byte("runtime: signal received on thread not created by Go.\n")
   274  
   275  func raisebadsignal(sig uint32) {
   276  	badsignal2()
   277  }
   278  
   279  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {}
   280  func munmap(addr unsafe.Pointer, n uintptr)               {}
   281  func setProcessCPUProfiler(hz int32)                      {}
   282  func setThreadCPUProfiler(hz int32)                       {}
   283  func sigdisable(uint32)                                   {}
   284  func sigenable(uint32)                                    {}
   285  func sigignore(uint32)                                    {}
   286  func closeonexec(int32)                                   {}
   287  
   288  var writelock uint32 // test-and-set spin lock for write
   289  
   290  /*
   291  An attempt at IRT. Doesn't work. See end of sys_nacl_amd64.s.
   292  
   293  void (*nacl_irt_query)(void);
   294  
   295  int8 nacl_irt_basic_v0_1_str[] = "nacl-irt-basic-0.1";
   296  void *nacl_irt_basic_v0_1[6]; // exit, gettod, clock, nanosleep, sched_yield, sysconf
   297  int32 nacl_irt_basic_v0_1_size = sizeof(nacl_irt_basic_v0_1);
   298  
   299  int8 nacl_irt_memory_v0_3_str[] = "nacl-irt-memory-0.3";
   300  void *nacl_irt_memory_v0_3[3]; // mmap, munmap, mprotect
   301  int32 nacl_irt_memory_v0_3_size = sizeof(nacl_irt_memory_v0_3);
   302  
   303  int8 nacl_irt_thread_v0_1_str[] = "nacl-irt-thread-0.1";
   304  void *nacl_irt_thread_v0_1[3]; // thread_create, thread_exit, thread_nice
   305  int32 nacl_irt_thread_v0_1_size = sizeof(nacl_irt_thread_v0_1);
   306  */