From a151198d11eb9887531e3ea57d362c96fd3f53c9 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Wed, 26 Aug 2020 10:43:59 +0800 Subject: [PATCH] Fix the golang demo's failure to run --- ...bos.patch => adapt-golang-to-occlum.patch} | 87 ++++++++++++++++++- tools/toolchains/golang/build.sh | 2 +- 2 files changed, 86 insertions(+), 3 deletions(-) rename tools/toolchains/golang/{0001-adapt-golang-to-occlum-libos.patch => adapt-golang-to-occlum.patch} (84%) diff --git a/tools/toolchains/golang/0001-adapt-golang-to-occlum-libos.patch b/tools/toolchains/golang/adapt-golang-to-occlum.patch similarity index 84% rename from tools/toolchains/golang/0001-adapt-golang-to-occlum-libos.patch rename to tools/toolchains/golang/adapt-golang-to-occlum.patch index 5566c542..4a22c43a 100644 --- a/tools/toolchains/golang/0001-adapt-golang-to-occlum-libos.patch +++ b/tools/toolchains/golang/adapt-golang-to-occlum.patch @@ -1,7 +1,7 @@ From 4c7e5586d109eba46c4f067699ae652d722270a0 Mon Sep 17 00:00:00 2001 From: "jeffery.wsj" Date: Fri, 8 May 2020 18:06:37 +0800 -Subject: [PATCH] adapt golang to occlum libos: +Subject: [PATCH 1/2] adapt golang to occlum libos: 1. hook heap malloc 2. hook all syscall based on amd64 linux platform @@ -633,5 +633,88 @@ index 364815df18..4417768e8a 100644 JLS ok7 NEGQ AX -- -2.19.1.3.ge56e4f7 +2.17.1 + + +From 4e78cdfbfbe4382a4dc9f2d4d7048f3c20bb1d74 Mon Sep 17 00:00:00 2001 +From: "jeffery.wsj" +Date: Fri, 8 May 2020 18:06:37 +0800 +Subject: [PATCH 2/2] refactor syscall ABI for occlum 0.15.0 + +--- + src/runtime/sys_linux_amd64.s | 4 ++-- + src/runtime/textflag.h | 23 +++++++++++++++-------- + src/syscall/asm_linux_amd64.s | 2 +- + 3 files changed, 18 insertions(+), 11 deletions(-) + +diff --git a/src/runtime/sys_linux_amd64.s b/src/runtime/sys_linux_amd64.s +index db9c9ab55b..6416170c41 100644 +--- a/src/runtime/sys_linux_amd64.s ++++ b/src/runtime/sys_linux_amd64.s +@@ -187,7 +187,7 @@ TEXT runtime·walltime(SB),NOSPLIT,$0-12 + MOVQ $SYS_clock_gettime, AX + MOVQ $0, DI + LEAQ ret+0(SP), SI +- CALL *runtime·occlumentry(SB) ++ OCCLUM_SYSCALL + MOVQ 0(SP), AX + MOVQ 8(SP), DX + MOVQ BP, SP +@@ -261,7 +261,7 @@ TEXT runtime·nanotime(SB),NOSPLIT,$0-8 + MOVQ $SYS_clock_gettime, AX + MOVQ $1, DI + LEAQ ret+0(SP), SI +- CALL *runtime·occlumentry(SB) ++ OCCLUM_SYSCALL + MOVQ 0(SP), AX // sec + MOVQ 8(SP), DX // nsec + IMULQ $1000000000, AX +diff --git a/src/runtime/textflag.h b/src/runtime/textflag.h +index 824f069076..e51f98849a 100644 +--- a/src/runtime/textflag.h ++++ b/src/runtime/textflag.h +@@ -36,13 +36,20 @@ + // at this function. + #define TOPFRAME 2048 + +-#define SYSCALL_ENHANCE \ +- CMPQ runtime·occlumentry(SB), $0x0 \ +- JBE 4(PC) \ +- CALL *runtime·occlumentry(SB) \ +- CMPQ runtime·occlumentry(SB), $0x0 \ +- JNE 2(PC) \ ++// refactor syscall ABI according to occlum v0.15.0. ++// step one: assign syscall return address to register rcx, occlum know where ++// should return according to rcx. ++// step two: jump to syscall interface address provided by occlum when go ++// .bin file loaded. ++// ++// actually is the assembler instruction: lea 0xc(%rip),%rcx ++#define OCCLUM_SYSCALL \ ++ BYTE $0x48; BYTE $0x8d; BYTE $0x0d; BYTE $0x0c; BYTE $0x00; BYTE $0x00; BYTE $0x00 \ ++ MOVQ runtime·occlumentry(SB), R11 \ ++ JMP R11 \ + SYSCALL + +-#define OCCLUM_GET_TIME_OF_DAY \ +- CALL *runtime·occlumentry(SB) ++#define SYSCALL_ENHANCE \ ++ CMPQ runtime·occlumentry(SB), $0x0 \ ++ JBE 10(PC) \ ++ OCCLUM_SYSCALL +diff --git a/src/syscall/asm_linux_amd64.s b/src/syscall/asm_linux_amd64.s +index 4417768e8a..06f2eb09c0 100644 +--- a/src/syscall/asm_linux_amd64.s ++++ b/src/syscall/asm_linux_amd64.s +@@ -157,7 +157,7 @@ TEXT ·gettimeofday(SB),NOSPLIT,$0-16 + MOVQ tv+0(FP), DI + MOVQ $0, SI + MOVQ $SYS_gettimeofday, AX +- OCCLUM_GET_TIME_OF_DAY ++ OCCLUM_SYSCALL + JMP result + + start: +-- +2.17.1 diff --git a/tools/toolchains/golang/build.sh b/tools/toolchains/golang/build.sh index a404ec85..ed999297 100755 --- a/tools/toolchains/golang/build.sh +++ b/tools/toolchains/golang/build.sh @@ -19,7 +19,7 @@ git clone https://github.com/golang/go . # Swtich to Golang 1.13.7 git checkout -b go1.13.7 tags/go1.13.7 # Apply the patch to adapt Golang to Occlum -git apply ${THIS_DIR}/0001-adapt-golang-to-occlum-libos.patch +git apply ${THIS_DIR}/adapt-golang-to-occlum.patch # Build Golang cd src