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) { void _exit(int status) {
#if defined(GEM5) #if defined(GEM5)
#if defined(__arm__) #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__) #elif defined(__aarch64__)
__asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);"); __asm__ __volatile__ ("mov x0, #0; .inst 0XFF000110 | (0x21 << 16);" : : : "x0");
#endif #endif
#else #else
#if defined(__arm__) #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__) #elif defined(__aarch64__)
/* TODO actually use the exit value here, just for fun. */ /* TODO actually use the exit value here, just for fun. */
__asm__ __volatile__ ( __asm__ __volatile__ (
@@ -82,6 +82,9 @@ void _exit(int status) {
"mov x1, sp\n" \ "mov x1, sp\n" \
"mov w0, #0x18\n" \ "mov w0, #0x18\n" \
"hlt 0xf000\n" "hlt 0xf000\n"
:
:
: "x0", "x1"
); );
#endif #endif
#endif #endif

View File

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

View File

@@ -2,7 +2,12 @@
#include <inttypes.h> #include <inttypes.h>
int main(void) { int main(void) {
register uint32_t x0 __asm__ ("x0"); uint32_t myvar = 1;
__asm__ ("mov x0, #1;" : : : "%x0"); __asm__ (
assert(x0 == 1); "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 in = 1;
uint32_t out = 0; uint32_t out = 0;
__asm__ ( __asm__ (
"movl %1, %%eax;" "mov %[in], %%eax;"
"inc %%eax;" "inc %%eax;"
"movl %%eax, %0" "mov %%eax, %[out]"
: "=m" (out) : [out] "=g" (out)
: "m" (in) : [in] "g" (in)
: "%eax" : "%eax"
); );
assert(out == in + 1); assert(out == in + 1);

View File

@@ -4,44 +4,44 @@
#if defined(__arm__) #if defined(__arm__)
static void m5_checkpoint(void) 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) 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() 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) 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) 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__) #elif defined(__aarch64__)
static void m5_checkpoint(void) 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) 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) 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) 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) 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 #else
static void m5_checkpoint(void) {}; static void m5_checkpoint(void) {};