Source file src/runtime/os_wasmx.go

     1  // Copyright 2023 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  //go:build wasm || wasm32
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/goarch"
    11  	"internal/runtime/atomic"
    12  	"unsafe"
    13  )
    14  
    15  func osinit() {
    16  	// https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances
    17  	physPageSize = 64 * 1024
    18  	if goarch.IsWasm32 != 0 && firstmoduledata.end > ^uintptr(0)-(physPageSize-1) {
    19  		throw("initial WebAssembly memory exceeds uintptr range")
    20  	}
    21  	initBloc()
    22  	pages := currentMemory()
    23  	if pages < 0 {
    24  		throw("negative WebAssembly memory size")
    25  	}
    26  	if goarch.IsWasm32 != 0 && uintptr(pages) > ^uintptr(0)/physPageSize {
    27  		// Only account for memory whose exclusive end fits in uintptr.
    28  		pages = int32(^uintptr(0) / physPageSize)
    29  	}
    30  	blocMax = uintptr(pages) * physPageSize // record the initial linear memory size
    31  	numCPUStartup = getCPUCount()
    32  	getg().m.procid = 2
    33  }
    34  
    35  func getCPUCount() int32 {
    36  	return 1
    37  }
    38  
    39  const _SIGSEGV = 0xb
    40  
    41  func sigpanic() {
    42  	gp := getg()
    43  	if !canpanic() {
    44  		throw("unexpected signal during runtime execution")
    45  	}
    46  
    47  	// js only invokes the exception handler for memory faults.
    48  	gp.sig = _SIGSEGV
    49  	panicmem()
    50  }
    51  
    52  // func exitThread(wait *uint32)
    53  // FIXME: wasm doesn't have atomic yet
    54  func exitThread(wait *atomic.Uint32)
    55  
    56  type mOS struct{}
    57  
    58  func osyield()
    59  
    60  //go:nosplit
    61  func osyield_no_g() {
    62  	osyield()
    63  }
    64  
    65  type sigset struct{}
    66  
    67  // Called to initialize a new m (including the bootstrap m).
    68  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
    69  func mpreinit(mp *m) {
    70  	mp.gsignal = malg(32 * 1024)
    71  	mp.gsignal.m = mp
    72  }
    73  
    74  //go:nosplit
    75  func usleep_no_g(usec uint32) {
    76  	usleep(usec)
    77  }
    78  
    79  //go:nosplit
    80  func sigsave(p *sigset) {
    81  }
    82  
    83  //go:nosplit
    84  func msigrestore(sigmask sigset) {
    85  }
    86  
    87  //go:nosplit
    88  //go:nowritebarrierrec
    89  func clearSignalHandlers() {
    90  }
    91  
    92  //go:nosplit
    93  func sigblock(exiting bool) {
    94  }
    95  
    96  // Called to initialize a new m (including the bootstrap m).
    97  // Called on the new thread, cannot allocate memory.
    98  func minit() {
    99  }
   100  
   101  // Called from dropm to undo the effect of an minit.
   102  func unminit() {
   103  }
   104  
   105  // Called from exitm, but not from drop, to undo the effect of thread-owned
   106  // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
   107  func mdestroy(mp *m) {
   108  }
   109  
   110  // wasm has no signals
   111  const _NSIG = 0
   112  
   113  func signame(sig uint32) string {
   114  	return ""
   115  }
   116  
   117  func crash() {
   118  	abort()
   119  }
   120  
   121  func initsig(preinit bool) {
   122  }
   123  
   124  // May run with m.p==nil, so write barriers are not allowed.
   125  //
   126  //go:nowritebarrier
   127  func newosproc(mp *m) {
   128  	throw("newosproc: not implemented")
   129  }
   130  
   131  // Do nothing on WASM platform, always return EPIPE to caller.
   132  //
   133  //go:linkname os_sigpipe os.sigpipe
   134  func os_sigpipe() {}
   135  
   136  //go:linkname syscall_now syscall.now
   137  func syscall_now() (sec int64, nsec int32) {
   138  	sec, nsec, _ = time_now()
   139  	return
   140  }
   141  
   142  //go:nosplit
   143  func cputicks() int64 {
   144  	// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
   145  	return nanotime()
   146  }
   147  
   148  // gsignalStack is unused on js.
   149  type gsignalStack struct{}
   150  
   151  const preemptMSupported = false
   152  
   153  func preemptM(gp *g) {
   154  	casfrom_Gscanstatus(gp, _Gscanrunning, _Grunning)
   155  
   156  	// No threads, so nothing to do.
   157  }
   158  
   159  // getfp returns the frame pointer register of its caller or 0 if not implemented.
   160  // TODO: Make this a compiler intrinsic
   161  //
   162  //go:nosplit
   163  func getfp() uintptr { return 0 }
   164  
   165  func setProcessCPUProfiler(hz int32) {}
   166  func setThreadCPUProfiler(hz int32)  {}
   167  func sigdisable(uint32)              {}
   168  func sigenable(uint32)               {}
   169  func sigignore(uint32)               {}
   170  
   171  // Stubs so tests can link correctly. These should never be called.
   172  func open(name *byte, mode, perm int32) int32        { panic("not implemented") }
   173  func closefd(fd int32) int32                         { panic("not implemented") }
   174  func read(fd int32, p unsafe.Pointer, n int32) int32 { panic("not implemented") }
   175  

View as plain text