Skip to content

Commit fbb1961

Browse files
committed
loongarch: add loongarch64 support for proot
1 parent 5f780cb commit fbb1961

File tree

7 files changed

+444
-3
lines changed

7 files changed

+444
-3
lines changed

src/arch.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef unsigned char byte_t;
3838
#define OFFSETOF_STATX_UID 20
3939
#define OFFSETOF_STATX_GID 24
4040

41-
#if !defined(ARCH_X86_64) && !defined(ARCH_ARM_EABI) && !defined(ARCH_X86) && !defined(ARCH_SH4)
41+
#if !defined(ARCH_X86_64) && !defined(ARCH_ARM_EABI) && !defined(ARCH_X86) && !defined(ARCH_SH4) && !defined(ARCH_LOONGARCH64)
4242
# if defined(__x86_64__)
4343
# define ARCH_X86_64 1
4444
# elif defined(__ARM_EABI__)
@@ -51,6 +51,8 @@ typedef unsigned char byte_t;
5151
# define ARCH_X86 1
5252
# elif defined(__SH4__)
5353
# define ARCH_SH4 1
54+
# elif defined(__loongarch_lp64)
55+
# define ARCH_LOONGARCH64 1
5456
# else
5557
# error "Unsupported architecture"
5658
# endif
@@ -170,6 +172,24 @@ typedef unsigned char byte_t;
170172
#define OFFSETOF_STAT_GID_32 0
171173
#define NO_MISALIGNED_ACCESS 1
172174

175+
#elif defined(ARCH_LOONGARCH64)
176+
177+
#define SYSNUMS_HEADER1 "syscall/sysnums-loongarch64.h"
178+
#define SYSNUMS_ABI1 sysnums_loongarch64
179+
180+
#define SYSTRAP_SIZE 4
181+
182+
#define SECCOMP_ARCHS { { .value = AUDIT_ARCH_LOONGARCH64, .nb_abis = 1, .abis = { ABI_DEFAULT } } }
183+
184+
#define HOST_ELF_MACHINE {258, 0};
185+
#define RED_ZONE_SIZE 0
186+
#define OFFSETOF_STAT_UID_32 0
187+
#define OFFSETOF_STAT_GID_32 0
188+
189+
#define LOADER_ADDRESS 0x2000000000
190+
#define EXEC_PIC_ADDRESS 0x3000000000
191+
#define INTERP_PIC_ADDRESS 0x3f00000000
192+
173193
#else
174194

175195
#error "Unsupported architecture"

src/loader/assembly-loongarch64.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2+
*
3+
* This file is part of PRoot.
4+
*
5+
* Copyright (C) 2015 STMicroelectronics
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation; either version 2 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful, but
13+
* WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20+
* 02110-1301 USA.
21+
*/
22+
23+
/* According to the LoongArch ABI, all registers have undefined values at
24+
* program startup except:
25+
*
26+
* - the instruction pointer (pc)
27+
* - the stack pointer (sp)
28+
* - the rtld_fini pointer (a0)
29+
*/
30+
#define BRANCH(stack_pointer, destination) do { \
31+
asm volatile ( \
32+
"# Restore initial stack pointer. \n\t" \
33+
"move $sp, %0 \n\t" \
34+
" \n\t" \
35+
"# Clear rtld_fini. \n\t" \
36+
"move $a0, $zero \n\t" \
37+
" \n\t" \
38+
"# Start the program. \n\t" \
39+
"jr %1 \n" \
40+
: /* no output */ \
41+
: "r" (stack_pointer), "r" (destination) \
42+
: "memory", "$sp", "$a0"); \
43+
__builtin_unreachable(); \
44+
} while (0)
45+
46+
#define PREPARE_ARGS_1(arg1_) \
47+
register word_t arg1 asm("$a0") = arg1_; \
48+
49+
#define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
50+
PREPARE_ARGS_1(arg1_) \
51+
register word_t arg2 asm("$a1") = arg2_; \
52+
register word_t arg3 asm("$a2") = arg3_; \
53+
54+
#define PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
55+
PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
56+
register word_t arg4 asm("$a3") = arg4_; \
57+
58+
#define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
59+
PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
60+
register word_t arg5 asm("$a4") = arg5_; \
61+
register word_t arg6 asm("$a5") = arg6_;
62+
63+
#define OUTPUT_CONTRAINTS_1 \
64+
"r" (arg1)
65+
66+
#define OUTPUT_CONTRAINTS_3 \
67+
OUTPUT_CONTRAINTS_1, \
68+
"r" (arg2), "r" (arg3)
69+
70+
#define OUTPUT_CONTRAINTS_4 \
71+
OUTPUT_CONTRAINTS_3, \
72+
"r" (arg4)
73+
74+
#define OUTPUT_CONTRAINTS_6 \
75+
OUTPUT_CONTRAINTS_4, \
76+
"r" (arg5), "r" (arg6)
77+
78+
#define SYSCALL_CLOBBERLIST \
79+
"memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8"
80+
81+
#define SYSCALL(number_, nb_args, args...) \
82+
({ \
83+
register word_t number asm("$a7") = number_; \
84+
register word_t result asm("$a0"); \
85+
PREPARE_ARGS_##nb_args(args) \
86+
asm volatile ( \
87+
"syscall 0 \n\t" \
88+
: "+r" (result) \
89+
: "r" (number), \
90+
OUTPUT_CONTRAINTS_##nb_args \
91+
: SYSCALL_CLOBBERLIST); \
92+
result; \
93+
})
94+
95+
#define OPENAT 56
96+
#define CLOSE 57
97+
#define MMAP 222
98+
#define EXECVE 221
99+
#define EXIT 93
100+
#define PRCTL 167
101+
#define MPROTECT 226

src/loader/loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
# include "loader/assembly-x86.h"
4444
#elif defined(ARCH_ARM64)
4545
# include "loader/assembly-arm64.h"
46+
#elif defined(ARCH_LOONGARCH64)
47+
# include "loader/assembly-loongarch64.h"
4648
#else
4749
# error "Unsupported architecture"
4850
#endif

src/ptrace/ptrace.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
#define user_fpregs_struct user_fpsimd_struct
5959
#endif
6060

61+
#if defined(ARCH_LOONGARCH64)
62+
#define user_fpregs_struct user_fp_struct
63+
#endif
64+
6165
static const char *stringify_ptrace(PTRACE_REQUEST_TYPE request)
6266
{
6367
#define CASE_STR(a) case a: return #a; break;

0 commit comments

Comments
 (0)