inline assembly: improve everywhere

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-02-25 00:00:00 +00:00
parent 7ec28d6808
commit a30ed0f047
5 changed files with 62 additions and 38 deletions

View File

@@ -64,13 +64,13 @@ int _write(int file, char *ptr, int len) {
void _exit(int status) {
#if defined(GEM5)
#if defined(__arm__)
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16);" : : : "r0", "r1");
#elif defined(__aarch64__)
__asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);");
__asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);" : : : "x0");
#endif
#else
#if defined(__arm__)
__asm__ __volatile__ ("mov r0, #0x18; ldr r1, =#0x20026; svc 0x00123456");
__asm__ __volatile__ ("mov r0, #0x18; ldr r1, =#0x20026; svc 0x00123456" : : : "r0", "r1");
#elif defined(__aarch64__)
/* TODO actually use the exit value here, just for fun. */
__asm__ __volatile__ (
@@ -82,6 +82,9 @@ void _exit(int status) {
"mov x1, sp\n" \
"mov w0, #0x18\n" \
"hlt 0xf000\n"
:
:
: "x0", "x1"
);
#endif
#endif

View File

@@ -27,30 +27,46 @@ void ring0_get_control_regs(Ring0Regs *ring0_regs)
#if defined(__x86_64__)
__asm__ __volatile__ (
"mov %%cr0, %%rax;"
"mov %%eax, %0;"
"mov %%cr2, %%rax;"
"mov %%eax, %1;"
"mov %%cr3, %%rax;"
"mov %%eax, %2;"
: "=m" (ring0_regs->cr0),
"=m" (ring0_regs->cr2),
"=m" (ring0_regs->cr3)
"mov %%eax, %[cr0];"
: [cr0] "=m" (ring0_regs->cr0)
:
: "%rax"
: "rax"
);
__asm__ __volatile__ (
"mov %%cr2, %%rax;"
"mov %%eax, %[cr2];"
: [cr2] "=m" (ring0_regs->cr2)
:
: "rax"
);
__asm__ __volatile__ (
"mov %%cr3, %%rax;"
"mov %%eax, %[cr3];"
: [cr3] "=m" (ring0_regs->cr3)
:
: "rax"
);
#elif defined(__i386__)
__asm__ __volatile__ (
"mov %%cr0, %%eax;"
"mov %%eax, %0;"
"mov %%cr2, %%eax;"
"mov %%eax, %1;"
"mov %%cr3, %%eax;"
"mov %%eax, %2;"
: "=m" (ring0_regs->cr0),
"=m" (ring0_regs->cr2),
"=m" (ring0_regs->cr3)
"mov %%eax, %[cr0];"
: [cr0] "=m" (ring0_regs->cr0)
:
: "%eax"
: "eax"
);
__asm__ __volatile__ (
"mov %%cr2, %%eax;"
"mov %%eax, %[cr2];"
: [cr2] "=m" (ring0_regs->cr2)
:
: "eax"
);
__asm__ __volatile__ (
"mov %%cr3, %%eax;"
"mov %%eax, %[cr3];"
: [cr3] "=m" (ring0_regs->cr3)
:
: "eax"
);
#endif
}

View File

@@ -2,7 +2,12 @@
#include <inttypes.h>
int main(void) {
register uint32_t x0 __asm__ ("x0");
__asm__ ("mov x0, #1;" : : : "%x0");
assert(x0 == 1);
uint32_t myvar = 1;
__asm__ (
"add %[myvar], %[myvar], 1;"
: [myvar] "=r" (myvar)
:
:
);
assert(myvar == 2);
}

View File

@@ -5,11 +5,11 @@ int main(void) {
uint32_t in = 1;
uint32_t out = 0;
__asm__ (
"movl %1, %%eax;"
"mov %[in], %%eax;"
"inc %%eax;"
"movl %%eax, %0"
: "=m" (out)
: "m" (in)
"mov %%eax, %[out]"
: [out] "=g" (out)
: [in] "g" (in)
: "%eax"
);
assert(out == in + 1);

View File

@@ -4,44 +4,44 @@
#if defined(__arm__)
static void m5_checkpoint(void)
{
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x43 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x43 << 16);" : : : "r0", "r1", "r2", "r3");
};
static void m5_dumpstats(void)
{
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x41 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x41 << 16);" : : : "r0", "r1", "r2", "r3");
};
static void m5_exit()
{
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16);" : : : "r0", "r1");
};
static void m5_fail_1(void)
{
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #1; mov r3, #0; .inst 0xEE000110 | (0x22 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #1; mov r3, #0; .inst 0xEE000110 | (0x22 << 16);" : : : "r0", "r1", "r2", "r3");
};
static void m5_resetstats(void)
{
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x40 << 16);");
__asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x40 << 16);" : : : "r0", "r1", "r2", "r3");
};
#elif defined(__aarch64__)
static void m5_checkpoint(void)
{
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0xFF000110 | (0x43 << 16);");
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0xFF000110 | (0x43 << 16);" : : : "x0", "x1");
};
static void m5_dumpstats(void)
{
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0xFF000110 | (0x41 << 16);");
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0xFF000110 | (0x41 << 16);" : : : "x0", "x1");
};
static void m5_exit(void)
{
__asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);");
__asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);" : : : "x0");
};
static void m5_fail_1(void)
{
__asm__ __volatile__ ("mov x0, #0; mov x1, #1; .inst 0xFF000110 | (0x22 << 16);");
__asm__ __volatile__ ("mov x0, #0; mov x1, #1; .inst 0xFF000110 | (0x22 << 16);" : : : "x0", "x1");
};
static void m5_resetstats(void)
{
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0XFF000110 | (0x40 << 16);");
__asm__ __volatile__ ("mov x0, #0; mov x1, #0; .inst 0XFF000110 | (0x40 << 16);" : : : "x0", "x1");
};
#else
static void m5_checkpoint(void) {};