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