Skip to content

Commit 832bdd7

Browse files
committed
add testing support for GOOS=tamago GOARCH=riscv64
1 parent afe7eff commit 832bdd7

7 files changed

+137
-9
lines changed

.github/workflows/build.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ jobs:
1414
- name: Compile Go with all.bash
1515
run: |
1616
cd src ; ./clean.bash ; ./all.bash
17-
- name: Run Go distribution tests under GOOS=tamago
17+
- name: Run Go distribution tests under tamago/arm
1818
run: |
1919
GO_BUILDER_NAME="tamago" GOOS=tamago GOARCH=arm ./bin/go tool dist test
20+
- name: Run Go distribution tests under tamago/riscv64
21+
run: |
22+
GO_BUILDER_NAME="tamago" GOOS=tamago GOARCH=riscv64 ./bin/go tool dist test

src/runtime/asm_riscv64.s

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,19 @@ TEXT setg_gcc<>(SB),NOSPLIT,$0-0
7979
CALL runtime·save_g(SB)
8080
RET
8181

82-
#ifndef GOOS_tamago
8382

8483
// func cputicks() int64
8584
TEXT runtime·cputicks(SB),NOSPLIT,$0-8
85+
#ifndef GOOS_tamago
8686
// RDTIME to emulate cpu ticks
8787
// RDCYCLE reads counter that is per HART(core) based
8888
// according to the riscv manual, see issue 46737
8989
RDTIME A0
9090
MOV A0, ret+0(FP)
9191
RET
92-
92+
#else
93+
// nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
94+
JMP runtime·nanotime(SB)
9395
#endif
9496

9597
// systemstack_switch is a dummy routine that systemstack leaves at the bottom

src/runtime/os_tamago_riscv64.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ func signame(sig uint32) string {
119119
return ""
120120
}
121121

122-
//go:nosplit
123-
func cputicks() int64 {
124-
// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
125-
return nanotime()
126-
}
122+
func cputicks() int64
127123

128124
//go:linkname os_sigpipe os.sigpipe
129125
func os_sigpipe() {

src/runtime/rt0_tamago_riscv64.s

+18
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,26 @@
1717
#define CSRS(RS,CSR) WORD $(0x2073 + RS<<15 + CSR<<20)
1818
#define CSRW(RS,CSR) WORD $(0x1073 + RS<<15 + CSR<<20)
1919

20+
#define SYS_mmap 222
21+
2022
// entry point for M privilege level instances
2123
TEXT _rt0_riscv64_tamago(SB),NOSPLIT|NOFRAME,$0
24+
MOV runtime·testBinary(SB), T0
25+
BEQ T0, ZERO, start
26+
27+
// when testing bare metal memory is mapped as OS virtual memory
28+
MOV runtime·ramStart(SB), A0
29+
MOV runtime·ramSize(SB), A1
30+
MOV $0x3, A2 // PROT_READ | PROT_WRITE
31+
MOV $0x22, A3 // MAP_PRIVATE | MAP_ANONYMOUS
32+
MOV $0xffffffff, A4
33+
MOV $0, A5
34+
MOV $SYS_mmap, A7
35+
ECALL
36+
37+
JMP _rt0_riscv64_tamago_start(SB)
38+
39+
start:
2240
// Disable interrupts
2341
MOV $0, T0
2442
CSRW (t0, sie)

src/runtime/sys_tamago_riscv64.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "go_tls.h"
1111
#include "textflag.h"
1212

13-
TEXT runtime·rt0_riscv64_tamago(SB),NOSPLIT|TOPFRAME,$0
13+
TEXT runtime·rt0_riscv64_tamago(SB),NOSPLIT|NOFRAME,$0
1414
// create istack out of the bootstack
1515
MOV $runtime·g0(SB), g
1616
MOV $(-64*1024), T0

src/testing/testing_tamago_riscv64.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021 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 tamago && riscv64
6+
7+
package testing
8+
9+
import (
10+
"runtime"
11+
_ "unsafe"
12+
)
13+
14+
//go:linkname ramStart runtime.ramStart
15+
var ramStart uint64 = 0x80000000
16+
17+
//go:linkname ramSize runtime.ramSize
18+
var ramSize uint64 = 0x20000000 // 512MB
19+
20+
//go:linkname ramStackOffset runtime.ramStackOffset
21+
var ramStackOffset uint64 = 0x100
22+
23+
// defined in testing_tamago_riscv64.s
24+
func sys_exit(code int32)
25+
func sys_write(c *byte)
26+
func sys_clock_gettime() (ns int64)
27+
func sys_getrandom(b []byte, n int)
28+
29+
//go:linkname nanotime1 runtime.nanotime1
30+
func nanotime1() int64 {
31+
return sys_clock_gettime()
32+
}
33+
34+
//go:linkname initRNG runtime.initRNG
35+
func initRNG() {}
36+
37+
//go:linkname getRandomData runtime.getRandomData
38+
func getRandomData(b []byte) {
39+
sys_getrandom(b, len(b))
40+
}
41+
42+
// preallocated memory to avoid malloc during panic
43+
var a [1]byte
44+
45+
//go:linkname printk runtime.printk
46+
func printk(c byte) {
47+
a[0] = c
48+
sys_write(&a[0])
49+
}
50+
51+
//go:linkname hwinit runtime.hwinit
52+
func hwinit() {
53+
runtime.Bloc = uintptr(ramStart)
54+
runtime.Exit = sys_exit
55+
}

src/testing/testing_tamago_riscv64.s

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2009 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 tamago && riscv64
6+
7+
#include "go_asm.h"
8+
#include "textflag.h"
9+
10+
#define CLOCK_REALTIME 0
11+
12+
#define SYS_write 64
13+
#define SYS_exit 93
14+
#define SYS_clock_gettime 113
15+
#define SYS_getrandom 278
16+
17+
// func sys_clock_gettime() int64
18+
TEXT ·sys_clock_gettime(SB),NOSPLIT,$40-8
19+
MOV $CLOCK_REALTIME, A0
20+
MOV $8(X2), A1
21+
MOV $SYS_clock_gettime, A7
22+
ECALL
23+
MOV 8(X2), T0 // sec
24+
MOV 16(X2), T1 // nsec
25+
MOV $1000000000, T2
26+
MUL T2, T0
27+
ADD T1, T0
28+
MOV T0, ns+0(FP)
29+
RET
30+
31+
// func sys_exit(code int32)
32+
TEXT ·sys_exit(SB), $0-4
33+
MOVW code+0(FP), A0
34+
MOV $SYS_exit, A7
35+
ECALL
36+
RET
37+
38+
// func sys_write(c *byte)
39+
TEXT ·sys_write(SB),NOSPLIT,$0-8
40+
MOV $1, A0 // fd
41+
MOV c+0(FP), A1 // p
42+
MOV $1, A2 // n
43+
MOV $SYS_write, A7
44+
ECALL
45+
RET
46+
47+
// func sys_getrandom(b []byte, n int)
48+
TEXT ·sys_getrandom(SB), $0-32
49+
MOV b+0(FP), A0
50+
MOV n+24(FP), A1
51+
MOV $0, A2
52+
MOV $SYS_getrandom, A7
53+
ECALL
54+
RET

0 commit comments

Comments
 (0)