Skip to content

Commit 0a55384

Browse files
committed
Add netbsd/arm64 support.
This works well enough to run basic commands natively.
1 parent dc3c01b commit 0a55384

14 files changed

+4681
-1
lines changed

src/runtime/cgo/gcc_netbsd_arm64.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
#include <sys/types.h>
6+
#include <pthread.h>
7+
#include <signal.h>
8+
#include <string.h>
9+
#include "libcgo.h"
10+
#include "libcgo_unix.h"
11+
12+
static void *threadentry(void*);
13+
14+
static void (*setg_gcc)(void*);
15+
16+
void
17+
x_cgo_init(G *g, void (*setg)(void*))
18+
{
19+
pthread_attr_t attr;
20+
size_t size;
21+
22+
setg_gcc = setg;
23+
pthread_attr_init(&attr);
24+
pthread_attr_getstacksize(&attr, &size);
25+
g->stacklo = (uintptr)&attr - size + 4096;
26+
pthread_attr_destroy(&attr);
27+
}
28+
29+
30+
void
31+
_cgo_sys_thread_start(ThreadStart *ts)
32+
{
33+
pthread_attr_t attr;
34+
sigset_t ign, oset;
35+
pthread_t p;
36+
size_t size;
37+
int err;
38+
39+
sigfillset(&ign);
40+
pthread_sigmask(SIG_SETMASK, &ign, &oset);
41+
42+
pthread_attr_init(&attr);
43+
pthread_attr_getstacksize(&attr, &size);
44+
// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
45+
ts->g->stackhi = size;
46+
err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
47+
48+
pthread_sigmask(SIG_SETMASK, &oset, nil);
49+
50+
if (err != 0) {
51+
fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
52+
abort();
53+
}
54+
}
55+
56+
static void*
57+
threadentry(void *v)
58+
{
59+
ThreadStart ts;
60+
stack_t ss;
61+
62+
ts = *(ThreadStart*)v;
63+
free(v);
64+
65+
// On NetBSD, a new thread inherits the signal stack of the
66+
// creating thread. That confuses minit, so we remove that
67+
// signal stack here before calling the regular mstart. It's
68+
// a bit baroque to remove a signal stack here only to add one
69+
// in minit, but it's a simple change that keeps NetBSD
70+
// working like other OS's. At this point all signals are
71+
// blocked, so there is no race.
72+
memset(&ss, 0, sizeof ss);
73+
ss.ss_flags = SS_DISABLE;
74+
sigaltstack(&ss, nil);
75+
76+
crosscall1(ts.fn, setg_gcc, (void*)ts.g);
77+
return nil;
78+
}

src/runtime/defs1_netbsd_arm64.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// created by cgo -cdefs and then converted to Go
2+
// cgo -cdefs defs_netbsd.go defs_netbsd_arm.go
3+
4+
package runtime
5+
6+
const (
7+
_EINTR = 0x4
8+
_EFAULT = 0xe
9+
10+
_PROT_NONE = 0x0
11+
_PROT_READ = 0x1
12+
_PROT_WRITE = 0x2
13+
_PROT_EXEC = 0x4
14+
15+
_MAP_ANON = 0x1000
16+
_MAP_PRIVATE = 0x2
17+
_MAP_FIXED = 0x10
18+
19+
_MADV_FREE = 0x6
20+
21+
_SA_SIGINFO = 0x40
22+
_SA_RESTART = 0x2
23+
_SA_ONSTACK = 0x1
24+
25+
_SIGHUP = 0x1
26+
_SIGINT = 0x2
27+
_SIGQUIT = 0x3
28+
_SIGILL = 0x4
29+
_SIGTRAP = 0x5
30+
_SIGABRT = 0x6
31+
_SIGEMT = 0x7
32+
_SIGFPE = 0x8
33+
_SIGKILL = 0x9
34+
_SIGBUS = 0xa
35+
_SIGSEGV = 0xb
36+
_SIGSYS = 0xc
37+
_SIGPIPE = 0xd
38+
_SIGALRM = 0xe
39+
_SIGTERM = 0xf
40+
_SIGURG = 0x10
41+
_SIGSTOP = 0x11
42+
_SIGTSTP = 0x12
43+
_SIGCONT = 0x13
44+
_SIGCHLD = 0x14
45+
_SIGTTIN = 0x15
46+
_SIGTTOU = 0x16
47+
_SIGIO = 0x17
48+
_SIGXCPU = 0x18
49+
_SIGXFSZ = 0x19
50+
_SIGVTALRM = 0x1a
51+
_SIGPROF = 0x1b
52+
_SIGWINCH = 0x1c
53+
_SIGINFO = 0x1d
54+
_SIGUSR1 = 0x1e
55+
_SIGUSR2 = 0x1f
56+
57+
_FPE_INTDIV = 0x1
58+
_FPE_INTOVF = 0x2
59+
_FPE_FLTDIV = 0x3
60+
_FPE_FLTOVF = 0x4
61+
_FPE_FLTUND = 0x5
62+
_FPE_FLTRES = 0x6
63+
_FPE_FLTINV = 0x7
64+
_FPE_FLTSUB = 0x8
65+
66+
_BUS_ADRALN = 0x1
67+
_BUS_ADRERR = 0x2
68+
_BUS_OBJERR = 0x3
69+
70+
_SEGV_MAPERR = 0x1
71+
_SEGV_ACCERR = 0x2
72+
73+
_ITIMER_REAL = 0x0
74+
_ITIMER_VIRTUAL = 0x1
75+
_ITIMER_PROF = 0x2
76+
77+
_EV_ADD = 0x1
78+
_EV_DELETE = 0x2
79+
_EV_CLEAR = 0x20
80+
_EV_RECEIPT = 0
81+
_EV_ERROR = 0x4000
82+
_EV_EOF = 0x8000
83+
_EVFILT_READ = 0x0
84+
_EVFILT_WRITE = 0x1
85+
)
86+
87+
type sigset struct {
88+
__bits [4]uint32
89+
}
90+
91+
type siginfo struct {
92+
_signo int32
93+
_code int32
94+
_errno int32
95+
_reason uintptr
96+
_reasonx [16]byte
97+
}
98+
99+
type stackt struct {
100+
ss_sp uintptr
101+
ss_size uintptr
102+
ss_flags int32
103+
}
104+
105+
type timespec struct {
106+
tv_sec int64
107+
tv_nsec int64
108+
}
109+
110+
func (ts *timespec) set_sec(x int32) {
111+
ts.tv_sec = int64(x)
112+
}
113+
114+
func (ts *timespec) set_nsec(x int32) {
115+
ts.tv_nsec = int64(x)
116+
}
117+
118+
type timeval struct {
119+
tv_sec int64
120+
tv_usec int32
121+
_ [4]byte // EABI
122+
}
123+
124+
func (tv *timeval) set_usec(x int32) {
125+
tv.tv_usec = x
126+
}
127+
128+
type itimerval struct {
129+
it_interval timeval
130+
it_value timeval
131+
}
132+
133+
type mcontextt struct {
134+
__gregs [35]uint64
135+
__fregs [4160]byte // _NFREG * 128 + 32 + 32
136+
_ [8]uint64 // future use
137+
}
138+
139+
type ucontextt struct {
140+
uc_flags uint32
141+
uc_link *ucontextt
142+
uc_sigmask sigset
143+
uc_stack stackt
144+
_ [4]byte // EABI
145+
uc_mcontext mcontextt
146+
__uc_pad [2]int32
147+
}
148+
149+
type keventt struct {
150+
ident uint64
151+
filter uint32
152+
flags uint32
153+
fflags uint32
154+
pad_cgo_0 [4]byte
155+
data int64
156+
udata *byte
157+
}
158+
159+
// created by cgo -cdefs and then converted to Go
160+
// cgo -cdefs defs_netbsd.go defs_netbsd_arm.go
161+
162+
const (
163+
_REG_X0 = 0
164+
_REG_X1 = 1
165+
_REG_X2 = 2
166+
_REG_X3 = 3
167+
_REG_X4 = 4
168+
_REG_X5 = 5
169+
_REG_X6 = 6
170+
_REG_X7 = 7
171+
_REG_X8 = 8
172+
_REG_X9 = 9
173+
_REG_X10 = 10
174+
_REG_X11 = 11
175+
_REG_X12 = 12
176+
_REG_X13 = 13
177+
_REG_X14 = 14
178+
_REG_X15 = 15
179+
_REG_X16 = 16
180+
_REG_X17 = 17
181+
_REG_X18 = 18
182+
_REG_X19 = 19
183+
_REG_X20 = 20
184+
_REG_X21 = 21
185+
_REG_X22 = 22
186+
_REG_X23 = 23
187+
_REG_X24 = 24
188+
_REG_X25 = 25
189+
_REG_X26 = 26
190+
_REG_X27 = 27
191+
_REG_X28 = 28
192+
_REG_X29 = 29
193+
_REG_X30 = 30
194+
_REG_X31 = 31
195+
_REG_ELR = 32
196+
_REG_SPSR = 33
197+
_REG_TPIDR = 34
198+
)

src/runtime/os_netbsd_arm64.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 runtime
6+
7+
import "unsafe"
8+
9+
func lwp_mcontext_init(mc *mcontextt, stk unsafe.Pointer, mp *m, gp *g, fn uintptr) {
10+
// Machine dependent mcontext initialisation for LWP.
11+
mc.__gregs[_REG_ELR] = uint64(funcPC(lwp_tramp))
12+
mc.__gregs[_REG_X31] = uint64(uintptr(stk))
13+
mc.__gregs[_REG_X0] = uint64(uintptr(unsafe.Pointer(mp)))
14+
mc.__gregs[_REG_X1] = uint64(uintptr(unsafe.Pointer(mp.g0)))
15+
mc.__gregs[_REG_X2] = uint64(fn)
16+
}
17+
18+
//go:nosplit
19+
func cputicks() int64 {
20+
// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
21+
// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
22+
// TODO: need more entropy to better seed fastrand.
23+
return nanotime()
24+
}

0 commit comments

Comments
 (0)