103 lines
3.3 KiB
C
103 lines
3.3 KiB
C
/*
|
|
* 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.
|
|
*
|
|
* -----------------------------------------------------------------------------
|
|
* File: System.h
|
|
* Description:
|
|
* This file contains specific system helper functions and constants for linux
|
|
* and windows.
|
|
*
|
|
* Author: Mineplay
|
|
* -----------------------------------------------------------------------------
|
|
*/
|
|
#ifndef IONY_SYSTEM
|
|
#define IONY_SYSTEM
|
|
|
|
#if defined(__GNUC__)
|
|
#if defined(__x86_64__)
|
|
#define IONY_SYS_READ 0
|
|
#define IONY_SYS_WRITE 1
|
|
#elif defined(__i386__)
|
|
#define IONY_SYS_READ 3
|
|
#define IONY_SYS_WRITE 4
|
|
#elif defined(__aarch64__)
|
|
#define IONY_SYS_READ 63
|
|
#define IONY_SYS_WRITE 64
|
|
#elif defined(__arm__)
|
|
#define IONY_SYS_READ 3
|
|
#define IONY_SYS_WRITE 4
|
|
#endif
|
|
|
|
static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) {
|
|
long result = 0;
|
|
#if defined(__x86_64__)
|
|
__asm__ volatile(
|
|
"movq %1, %%rax\n"
|
|
"movq %2, %%rdi\n"
|
|
"movq %3, %%rsi\n"
|
|
"movq %4, %%rdx\n"
|
|
"movq %5, %%r10\n"
|
|
"movq %6, %%r8\n"
|
|
"movq %7, %%r9\n"
|
|
"syscall\n"
|
|
"movq %%rax, %0\n"
|
|
: "=r"(result)
|
|
: "r"(number), "r"(argument1), "r"(argument2), "r"(argument3), "r"(argument4), "r"(argument5), "r"(argument6)
|
|
: "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory"
|
|
);
|
|
#elif defined(__i386__)
|
|
__asm__ volatile(
|
|
"movl %1, %%eax\n"
|
|
"movl %2, %%ebx\n"
|
|
"movl %3, %%ecx\n"
|
|
"movl %4, %%edx\n"
|
|
"movl %5, %%esi\n"
|
|
"movl %6, %%edi\n"
|
|
"movl %7, %%ebp\n"
|
|
"int $0x80\n"
|
|
"movl %%eax, %0\n"
|
|
: "=r"(result)
|
|
: "r"(number), "r"(argument1), "r"(argument2), "r"(argument3),"r"(argument4), "r"(argument5), "r"(argument6)
|
|
: "eax","ebx","ecx","edx","esi","edi","ebp","memory"
|
|
);
|
|
#elif defined(__aarch64__)
|
|
register long x0 asm("x0") = argument1;
|
|
register long x1 asm("x1") = argument2;
|
|
register long x2 asm("x2") = argument3;
|
|
register long x3 asm("x3") = argument4;
|
|
register long x4 asm("x4") = argument5;
|
|
register long x5 asm("x5") = argument6;
|
|
register long x8 asm("x8") = number;
|
|
|
|
__asm__ volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory");
|
|
result = x0;
|
|
#elif defined(__arm__)
|
|
register long r0 asm("r0") = argument1;
|
|
register long r1 asm("r1") = argument2;
|
|
register long r2 asm("r2") = argument3;
|
|
register long r3 asm("r3") = argument4;
|
|
register long r4 asm("r4") = argument5;
|
|
register long r5 asm("r5") = argument6;
|
|
register long r7 asm("r7") = number;
|
|
|
|
__asm__ volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory");
|
|
result = r0;
|
|
#else
|
|
#error "Unsupported architecture"
|
|
#endif
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
#endif
|