Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
use github.com/petermattis/goid to impl syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 8, 2022
1 parent 77679e5 commit f3c8429
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 33 deletions.
6 changes: 5 additions & 1 deletion c2go.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
"ignore": {
"names": [
"a_cas", "a_ll", "a_sc", "a_swap",
"__syscall0", "__syscall0_r1",
"__syscall1", "__syscall2", "__syscall3",
"__syscall4", "__syscall5", "__syscall6",
"__syscall_ret"
"__syscall1_r1", "__syscall2_r1", "__syscall3_r1",
"__syscall4_r1", "__syscall5_r1", "__syscall6_r1",
"__syscall_ret",
"__pthread_self"
],
"paths": [
"syscall_ret.c"
Expand Down
4 changes: 2 additions & 2 deletions c2go/include/atomic_arch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _C2GO_ATOMIC_H
#define _C2GO_ATOMIC_H
#ifndef _C2GO_ATOMIC_ARCH_H
#define _C2GO_ATOMIC_ARCH_H

#define a_ll a_ll
int a_ll(volatile int *p);
Expand Down
4 changes: 3 additions & 1 deletion c2go/include/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#define ___errno_location __errno_location

#define UNISTD_H // don't include old unistd.h
#define _PTHREAD_IMPL_H // don't include old pthread_impl.h
#define _INTERNAL_SYSCALL_H // don't include old internal/syscall.h
#define _STDIO_IMPL_H
#define _PTHREAD_IMPL_H
#include "pthread_impl.h"

#endif // _C2GO_FEATURES_H
6 changes: 3 additions & 3 deletions c2go/include/pthread_arch.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef _C2GO_PTHREAD_H
#define _C2GO_PTHREAD_H
#ifndef _C2GO_PTHREAD_ARCH_H
#define _C2GO_PTHREAD_ARCH_H

// uintptr_t __get_tp();
//
// #define TLS_ABOVE_TP
// #define GAP_ABOVE_TP 8
// #define MC_PC arm_pc

#endif // _C2GO_PTHREAD_H
#endif
1 change: 1 addition & 0 deletions c2go/include/pthread_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
struct pthread {
int tid;
int errno_val;
long sys_r1; // syscall_ret r1
locale_t locale;
};

Expand Down
11 changes: 11 additions & 0 deletions c2go/include/stdio_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _C2GO_STDIO_IMPL_H
#define _C2GO_STDIO_IMPL_H

#ifndef _C2GO_SYSCALL_H
#include "syscall.h"
#endif

#undef _STDIO_IMPL_H
#include "../../src/internal/stdio_impl.h"

#endif
27 changes: 27 additions & 0 deletions c2go/include/syscall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _C2GO_SYSCALL_H
#define _C2GO_SYSCALL_H

#ifndef _C2GO_SYSCALL_ARCH_H
#include "syscall_arch.h"
#endif

#undef _INTERNAL_SYSCALL_H
#include "../../src/internal/syscall.h"

#define __SYSCALL_R1_DISP(b,...) \
__SYSCALL_CONCAT(__SYSCALL_CONCAT(b,__SYSCALL_NARGS(__VA_ARGS__)),_r1)(__VA_ARGS__)

#define __syscall_r1(...) __SYSCALL_R1_DISP(__syscall,__VA_ARGS__)

#define __syscall1_r1(n,a) __syscall1_r1(n,__scc(a))
#define __syscall2_r1(n,a,b) __syscall2_r1(n,__scc(a),__scc(b))
#define __syscall3_r1(n,a,b,c) __syscall3_r1(n,__scc(a),__scc(b),__scc(c))
#define __syscall4_r1(n,a,b,c,d) __syscall4_r1(n,__scc(a),__scc(b),__scc(c),__scc(d))
#define __syscall5_r1(n,a,b,c,d,e) __syscall5_r1(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e))
#define __syscall6_r1(n,a,b,c,d,e,f) __syscall6_r1(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f))
#define __syscall7_r1(n,a,b,c,d,e,f,g) __syscall7_r1(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f),__scc(g))

#undef syscall
#define syscall(...) __syscall_r1(__VA_ARGS__)

#endif
12 changes: 10 additions & 2 deletions c2go/include/syscall_arch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _C2GO_SYSCALL_H
#define _C2GO_SYSCALL_H
#ifndef _C2GO_SYSCALL_ARCH_H
#define _C2GO_SYSCALL_ARCH_H

long __syscall0(long n);
long __syscall1(long n, long a);
Expand All @@ -9,4 +9,12 @@ long __syscall4(long n, long a, long b, long c, long d);
long __syscall5(long n, long a, long b, long c, long d, long e);
long __syscall6(long n, long a, long b, long c, long d, long e, long f);

long __syscall0_r1(long n);
long __syscall1_r1(long n, long a);
long __syscall2_r1(long n, long a, long b);
long __syscall3_r1(long n, long a, long b, long c);
long __syscall4_r1(long n, long a, long b, long c, long d);
long __syscall5_r1(long n, long a, long b, long c, long d, long e);
long __syscall6_r1(long n, long a, long b, long c, long d, long e, long f);

#endif
2 changes: 1 addition & 1 deletion c2go__map_file.c.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func __map_file(pathname *int8, size *uint64) *uint8 {
if fd < 0 {
return (*uint8)(nil)
}
if !(__syscall_ret(uint64(__syscall2(int64(339), int64(fd), int64(uintptr(unsafe.Pointer(&st)))))) != 0) {
if !(__syscall2_r1(int64(339), int64(fd), int64(uintptr(unsafe.Pointer(&st)))) != 0) {
map_ = (*uint8)(__mmap(nil, uint64(st.st_size), 1, 1, fd, int64(0)))
*size = uint64(st.st_size)
}
Expand Down
2 changes: 1 addition & 1 deletion c2go__stdio_close.c.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ func dummy_cgo755(fd int32) int32 {
return fd
}
func __stdio_close(f *struct__IO_FILE) int32 {
return int32(__syscall_ret(uint64(__syscall1(int64(6), int64(__aio_close(f.fd))))))
return int32(__syscall1_r1(int64(6), int64(__aio_close(f.fd))))
}
4 changes: 2 additions & 2 deletions c2go__stdio_read.c.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func __stdio_read(f *struct__IO_FILE, buf *uint8, len uint64) uint64 {
var cnt int64
cnt = func() int64 {
if (*(*struct_iovec)(unsafe.Pointer(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov)))) + uintptr(0)*16))).iov_len != 0 {
return __syscall_ret(uint64(__syscall3(int64(120), int64(f.fd), int64(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov))))), int64(2))))
return __syscall3_r1(int64(120), int64(f.fd), int64(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov))))), int64(2))
} else {
return __syscall_ret(uint64(__syscall3(int64(3), int64(f.fd), int64(uintptr((*(*struct_iovec)(unsafe.Pointer(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov)))) + uintptr(1)*16))).iov_base)), int64((*(*struct_iovec)(unsafe.Pointer(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov)))) + uintptr(1)*16))).iov_len))))
return __syscall3_r1(int64(3), int64(f.fd), int64(uintptr((*(*struct_iovec)(unsafe.Pointer(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov)))) + uintptr(1)*16))).iov_base)), int64((*(*struct_iovec)(unsafe.Pointer(uintptr(unsafe.Pointer((*struct_iovec)(unsafe.Pointer(&iov)))) + uintptr(1)*16))).iov_len))
}
}()
if cnt <= int64(0) {
Expand Down
2 changes: 1 addition & 1 deletion c2go__stdio_write.c.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func __stdio_write(f *struct__IO_FILE, buf *uint8, len uint64) uint64 {
var iovcnt int32 = 2
var cnt int64
for {
cnt = __syscall_ret(uint64(__syscall3(int64(121), int64(f.fd), int64(uintptr(unsafe.Pointer(iov))), int64(iovcnt))))
cnt = __syscall3_r1(int64(121), int64(f.fd), int64(uintptr(unsafe.Pointer(iov))), int64(iovcnt))
if uint64(cnt) == rem {
f.wend = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(f.buf)) + uintptr(f.buf_size)))
f.wpos = func() (_cgo_ret *uint8) {
Expand Down
3 changes: 0 additions & 3 deletions c2go_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ func __libc_malloc(uint64) unsafe.Pointer {
func __lock(*int32) {
panic("notimpl")
}
func __pthread_self() *struct___pthread {
panic("notimpl")
}
func __syscall_cp(int64, int64, int64, int64, int64, int64, int64) int64 {
panic("notimpl")
}
Expand Down
1 change: 1 addition & 0 deletions c2go_header.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type struct___ptcb struct {
type struct___pthread struct {
tid int32
errno_val int32
sys_r1 int64
locale *struct___locale_struct
}
type wchar_t = uint32
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/goplus/libc

go 1.16

require github.com/goplus/c2go v0.6.2
require (
github.com/goplus/c2go v0.6.2
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/goplus/gox v1.11.3/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/K
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a h1:VXRRto5GMJPNfB7MNbUVoFhtxwoYjBEsIt/NpWg42U0=
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qiniu/x v1.11.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion lseek.c.i.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package libc

func __lseek(fd int32, offset int64, whence int32) int64 {
return int64(__syscall_ret(uint64(__syscall3(int64(199), int64(fd), int64(offset), int64(whence)))))
return int64(__syscall3_r1(int64(199), int64(fd), int64(offset), int64(whence)))
}
2 changes: 1 addition & 1 deletion munmap.c.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ func dummy_cgo489() {
}
func __munmap(start unsafe.Pointer, len uint64) int32 {
__vm_wait()
return int32(__syscall_ret(uint64(__syscall2(int64(73), int64(uintptr(start)), int64(len)))))
return int32(__syscall2_r1(int64(73), int64(uintptr(start)), int64(len)))
}
24 changes: 24 additions & 0 deletions pthread.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package libc

import (
"sync"

"github.com/petermattis/goid"
)

var (
gtls sync.Map
)

func __pthread_self() *struct___pthread {
var self *struct___pthread
id := goid.Get()
ret, ok := gtls.Load(id)
if !ok {
self = &struct___pthread{tid: int32(id)}
gtls.Store(id, self)
} else {
self = ret.(*struct___pthread)
}
return self
}
73 changes: 60 additions & 13 deletions syscall.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,86 @@
package libc

import "syscall"
import (
"syscall"
)

var g_r1 uintptr // TODO: goroutine safe
func __syscall0(n int64) int64 {
r1, _, ret := syscall.Syscall(uintptr(n), 0, 0, 0)
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall0_r1(n int64) int64 {
r1, _, _ := syscall.Syscall(uintptr(n), 0, 0, 0)
return int64(r1)
}

func __syscall1(n int64, a int64) int64 {
var ret syscall.Errno
g_r1, _, ret = syscall.Syscall(uintptr(n), uintptr(a), 0, 0)
r1, _, ret := syscall.Syscall(uintptr(n), uintptr(a), 0, 0)
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall1_r1(n int64, a int64) int64 {
r1, _, _ := syscall.Syscall(uintptr(n), uintptr(a), 0, 0)
return int64(r1)
}

func __syscall2(n int64, a, b int64) int64 {
var ret syscall.Errno
g_r1, _, ret = syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), 0)
r1, _, ret := syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), 0)
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall2_r1(n int64, a, b int64) int64 {
r1, _, _ := syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), 0)
return int64(r1)
}

func __syscall3(n int64, a int64, b int64, c int64) int64 {
var ret syscall.Errno
g_r1, _, ret = syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), uintptr(c))
r1, _, ret := syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), uintptr(c))
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall3_r1(n int64, a int64, b int64, c int64) int64 {
r1, _, _ := syscall.Syscall(uintptr(n), uintptr(a), uintptr(b), uintptr(c))
return int64(r1)
}

func __syscall4(n int64, a int64, b int64, c int64, d int64) int64 {
var ret syscall.Errno
g_r1, _, ret = syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), 0, 0)
r1, _, ret := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), 0, 0)
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall4_r1(n int64, a int64, b int64, c int64, d int64) int64 {
r1, _, _ := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), 0, 0)
return int64(r1)
}

func __syscall5(n int64, a int64, b int64, c int64, d int64, e int64) int64 {
r1, _, ret := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), uintptr(e), 0)
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall5_r1(n int64, a int64, b int64, c int64, d int64, e int64) int64 {
r1, _, _ := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), uintptr(e), 0)
return int64(r1)
}

func __syscall6(n int64, a int64, b int64, c int64, d int64, e int64, f int64) int64 {
var ret syscall.Errno
g_r1, _, ret = syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), uintptr(e), uintptr(f))
r1, _, ret := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), uintptr(e), uintptr(f))
__pthread_self().sys_r1 = int64(r1)
return int64(ret)
}

func __syscall6_r1(n int64, a int64, b int64, c int64, d int64, e int64, f int64) int64 {
r1, _, _ := syscall.Syscall6(uintptr(n), uintptr(a), uintptr(b), uintptr(c), uintptr(d), uintptr(e), uintptr(f))
return int64(r1)
}

func __syscall_ret(uint64) int64 {
return int64(g_r1)
return __pthread_self().sys_r1
}

0 comments on commit f3c8429

Please sign in to comment.