Skip to content

Commit

Permalink
Initial loongarch port (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangzhai authored Dec 1, 2023
1 parent 8d77e09 commit 104b7bd
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,15 @@ else
ARCH_TAG=ppc64le
else ifeq ($(ARCH),riscv64)
ARCH_TAG=riscv64
else ifeq ($(ARCH),loongarch64)
ARCH_TAG=loongarch64
else
ARCH_TAG=x86
endif
endif

ifneq ($(ARCH),ppc64le)
ifneq ($(ARCH_TAG),arm32)
ifneq ($(ARCH_TAG),riscv64)
CXXFLAGS += -momit-leaf-frame-pointer
endif
endif
ifneq (,$(findstring $(ARCH_TAG),x86 x64 arm64))
CXXFLAGS += -momit-leaf-frame-pointer
endif


Expand Down
17 changes: 17 additions & 0 deletions src/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ const int PERF_REG_PC = 0; // PERF_REG_RISCV_PC
#define rmb() asm volatile ("fence" : : : "memory")
#define flushCache(addr) __builtin___clear_cache((char*)(addr), (char*)(addr) + sizeof(instruction_t))

#elif defined(__loongarch_lp64)

typedef unsigned int instruction_t;
const instruction_t BREAKPOINT = 0x002a0005; // EBREAK
const int BREAKPOINT_OFFSET = 0;

const int SYSCALL_SIZE = sizeof(instruction_t);
const int FRAME_PC_SLOT = 1;
const int PROBE_SP_LIMIT = 0;
const int PLT_HEADER_SIZE = 32;
const int PLT_ENTRY_SIZE = 16;
const int PERF_REG_PC = 0; // PERF_REG_LOONGARCH_PC

#define spinPause() asm volatile("ibar 0x0")
#define rmb() asm volatile("dbar 0x0" : : : "memory")
#define flushCache(addr) __builtin___clear_cache((char*)(addr), (char*)(addr) + sizeof(instruction_t))

#else

#error "Compiling on unsupported arch"
Expand Down
108 changes: 108 additions & 0 deletions src/stackFrame_loongarch64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2021 Andrei Pangin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifdef __loongarch_lp64

#include <errno.h>
#include <string.h>
#include <sys/syscall.h>
#include "stackFrame.h"

#define REG(l) _ucontext->uc_mcontext.__gregs[l]

uintptr_t& StackFrame::pc() {
return (uintptr_t&)_ucontext->uc_mcontext.__pc;
}

uintptr_t& StackFrame::sp() {
return (uintptr_t&)REG(LARCH_REG_SP);
}

uintptr_t& StackFrame::fp() {
return (uintptr_t&)REG(22);
}

uintptr_t& StackFrame::retval() {
return (uintptr_t&)REG(LARCH_REG_A0);
}

uintptr_t StackFrame::link() {
return (uintptr_t)REG(LARCH_REG_RA);
}

uintptr_t StackFrame::arg0() {
return (uintptr_t)REG(LARCH_REG_A0);
}

uintptr_t StackFrame::arg1() {
return (uintptr_t)REG(LARCH_REG_A0 + 1);
}

uintptr_t StackFrame::arg2() {
return (uintptr_t)REG(LARCH_REG_A0 + 2);
}

uintptr_t StackFrame::arg3() {
return (uintptr_t)REG(LARCH_REG_A0 + 3);
}

uintptr_t StackFrame::jarg0() {
return (uintptr_t)REG(12);
}

uintptr_t StackFrame::method() {
return (uintptr_t)REG(26);
}

uintptr_t StackFrame::senderSP() {
return (uintptr_t)REG(27);
}

void StackFrame::ret() {
pc() = link();
}

bool StackFrame::unwindStub(instruction_t* entry, const char* name, uintptr_t& pc, uintptr_t& sp, uintptr_t& fp) {
instruction_t* ip = (instruction_t*)pc;
if (ip == entry
|| strncmp(name, "itable", 6) == 0
|| strncmp(name, "vtable", 6) == 0
|| strcmp(name, "InlineCacheBuffer") == 0)
{
pc = link();
return true;
}
return false;
}

bool StackFrame::unwindCompiled(instruction_t* entry, uintptr_t& pc, uintptr_t& sp, uintptr_t& fp) {
// Not yet implemented
return false;
}

bool StackFrame::skipFaultInstruction() {
return false;
}

bool StackFrame::checkInterruptedSyscall() {
return retval() == (uintptr_t)-EINTR;
}

bool StackFrame::isSyscall(instruction_t* pc) {
return (*pc) == 0x002b0000;
}

#endif // __loongarch_lp64
4 changes: 4 additions & 0 deletions src/symbols_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ typedef Elf32_Dyn ElfDyn;
// RISC-V does not have GLOB_DAT relocation, use something neutral,
// like the impossible relocation number.
# define R_GLOB_DAT -1
#elif defined(__loongarch_lp64)
// LOONGARCH does not have GLOB_DAT relocation, use something neutral,
// like the impossible relocation number.
# define R_GLOB_DAT -1
#else
# error "Compiling on unsupported arch"
#endif
Expand Down

0 comments on commit 104b7bd

Please sign in to comment.