|
| 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 |
0 commit comments