Compare commits
No commits in common. "31df6146bfe0f9431c823afa583ad71b2289560b" and "415dfd9fa4007e879a4f1b613001af32360ddbea" have entirely different histories.
31df6146bf
...
415dfd9fa4
14
Makefile
14
Makefile
@ -1,4 +1,4 @@
|
||||
all: buildtest
|
||||
all: print print_x64 print_aarch64 print_mips32 buildtest
|
||||
|
||||
clean:
|
||||
if test -f print; then rm print; fi
|
||||
@ -15,6 +15,18 @@ buildtest: bldtst bldtst_llvm bldtst_x64 bldtst_aarch64 bldtst_mips32
|
||||
|
||||
.PHONY: all buildtest clean
|
||||
|
||||
print:
|
||||
gcc -static -nostdlib -Wno-builtin-declaration-mismatch -fno-stack-protector -o print print.c
|
||||
print_x64: print.c
|
||||
clang --target=x86_64-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -o print_x64 print.c
|
||||
|
||||
print_aarch64: print.c
|
||||
clang --target=aarch64-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -o print_aarch64 print.c
|
||||
|
||||
print_mips32: print.c
|
||||
clang --target=mips-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -fno-pic -o print_mips32 print.c
|
||||
|
||||
|
||||
|
||||
bldtst: buildtest.c arch/*.c
|
||||
gcc -static -nostdlib -Wno-builtin-declaration-mismatch -fno-stack-protector -o bldtst buildtest.c arch/*.c
|
||||
|
||||
@ -1,50 +1,34 @@
|
||||
#include <stdint.h>
|
||||
#if defined(__x86_64__)
|
||||
|
||||
#define SYS_EXIT 60
|
||||
void exit(int8_t status) {
|
||||
asm volatile(
|
||||
"syscall"
|
||||
:
|
||||
: "a"(SYS_EXIT), "D"(status)
|
||||
: "a"(60), "D"(status)
|
||||
:
|
||||
);
|
||||
}
|
||||
|
||||
#define SYS_WRITE 1
|
||||
intptr_t write(int32_t fd, const void* buf, intptr_t size) {
|
||||
intptr_t n_written = 0;
|
||||
asm volatile (
|
||||
"syscall\n"
|
||||
"movq %0, %%rax\n"
|
||||
: "=r"(n_written)
|
||||
: "a"(SYS_WRITE), "D"(fd), "S"(buf), "d"(size) // RDI, RSI, RDX are used in x86-64 for write args
|
||||
: "a"(1), "D"(fd), "S"(buf), "d"(size) // RDI, RSI, RDX are used in x86-64 for write args
|
||||
: "rcx", "r11", "memory"
|
||||
);
|
||||
return n_written;
|
||||
}
|
||||
|
||||
#define SYS_READ 0
|
||||
intptr_t read(int32_t fd, const void* buf, intptr_t size) {
|
||||
intptr_t n_read = 0;
|
||||
asm volatile (
|
||||
"syscall\n"
|
||||
"movq %0, %%rax\n"
|
||||
: "=r"(n_read)
|
||||
: "a"(SYS_READ), "D"(fd), "S"(buf), "d"(size) // RDI, RSI, RDX are used in x86-64 for write args
|
||||
: "rcx", "r11", "memory"
|
||||
);
|
||||
return n_read;
|
||||
}
|
||||
|
||||
#define SYS_FORK 57
|
||||
uint32_t fork() {
|
||||
uint64_t rtn;
|
||||
asm volatile(
|
||||
"syscall\n" // syscall
|
||||
"movq %0, %%rax\n" // save return value
|
||||
: "=r"(rtn)
|
||||
: "a"(SYS_FORK)
|
||||
: "a"(57)
|
||||
:
|
||||
);
|
||||
return (uint32_t)rtn;
|
||||
|
||||
38
buildtest.c
38
buildtest.c
@ -3,9 +3,8 @@
|
||||
|
||||
|
||||
int main() {
|
||||
#if defined(__x86_64__) || defined(__aarch64__) || defined(__mips__)
|
||||
uint64_t pid = fork();
|
||||
|
||||
// Print the pid in hex
|
||||
char msg[17] = {' '};
|
||||
msg[16] = '\n';
|
||||
for(int i = 0; i < 16; ++i) {
|
||||
@ -16,23 +15,12 @@ int main() {
|
||||
msg[i] = c;
|
||||
}
|
||||
write(STDIO, msg, 17);
|
||||
|
||||
// Child process exits
|
||||
if(pid == 0) return 0;
|
||||
//TODO: wait on child to remove zombie process
|
||||
|
||||
#if defined(__x86_64__)
|
||||
// Test the read syscall
|
||||
#define INPUT_BUFFER_LEN 4096
|
||||
char input_buffer[INPUT_BUFFER_LEN] = {0};
|
||||
write(STDIO, "Enter some text:", 16);
|
||||
intptr_t n_read = read(STDIO, input_buffer, INPUT_BUFFER_LEN);
|
||||
write(STDIO, input_buffer, n_read);
|
||||
#endif
|
||||
|
||||
#else
|
||||
char *msg = "Hello World!\n";
|
||||
write(STDIO, msg, 13);
|
||||
#endif
|
||||
return 69;
|
||||
}
|
||||
|
||||
void __libc_start_main() {exit(main());}
|
||||
|
||||
void _start() {
|
||||
@ -42,3 +30,19 @@ void _start() {
|
||||
void __start() {
|
||||
_start();
|
||||
}
|
||||
/*
|
||||
#if defined(__x86_64__)
|
||||
printf("Compiled for x86_64 architecture\n");
|
||||
#elif defined(__i386__)
|
||||
printf("Compiled for x86 architecture\n");
|
||||
#elif defined(__arm__)
|
||||
printf("Compiled for ARM architecture\n");
|
||||
#elif defined(__aarch64__)
|
||||
printf("Compiled for ARM64 architecture\n");
|
||||
#elif defined(__mips__)
|
||||
printf("Compiled for ARM architecture\n");
|
||||
#else
|
||||
printf("Unknown architecture\n");
|
||||
#endif
|
||||
*/
|
||||
|
||||
|
||||
173
print.c
Normal file
173
print.c
Normal file
@ -0,0 +1,173 @@
|
||||
long print(const void *buf, long count) {
|
||||
#if defined(__x86_64__)
|
||||
asm volatile (
|
||||
"syscall"
|
||||
:
|
||||
: "a"(1), "D"(1), "S"(buf), "d"(count) // RDI, RSI, RDX are used in x86-64 for write args
|
||||
: "rcx", "r11", "memory"
|
||||
);
|
||||
#elif defined(__aarch64__)
|
||||
asm ( "mov x0, #1\n" // Move the exit status into register x0
|
||||
"mov x1, %0\n" // Syscall number for 'exit' is 93 in AArch64
|
||||
"mov x2, %1\n" // Syscall number for 'exit' is 93 in AArch64
|
||||
"mov x8, #64\n" // Syscall number for 'exit' is 93 in AArch64
|
||||
"svc #0\n" // Make the syscall
|
||||
: // No output operands
|
||||
: "r" (buf), "r"(count) // Input operand: status
|
||||
: "x0", "x8" // Clobbered registers
|
||||
);
|
||||
#elif defined(__mips__)
|
||||
asm ( "li $a0, 1\n"
|
||||
"move $a1, %0\n"
|
||||
"move $a2, %1\n"
|
||||
"li $v0, 4004\n"
|
||||
"syscall"
|
||||
:
|
||||
: "r"(buf), "r"(count)
|
||||
: "a0", "a1", "a2", "v0" //TODO: temp registers clobbered by syscall should probably also be listed but this fn returns right away so it should be fine?
|
||||
);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define CLONE_CHILD_SETTID 0x1000000
|
||||
#define CLONE_CHILD_CLEARTID 0x200000
|
||||
#if defined(__aarch64__)
|
||||
long int fork() {
|
||||
long int rtn;
|
||||
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
|
||||
asm volatile (
|
||||
// Assembly Instructions
|
||||
"mov x8, #220\n" // Syscall number for 'clone' is 220 in AArch64
|
||||
"mov x0, %1\n" // clone flags
|
||||
"mov x1, #0\n" // child stack pointer
|
||||
"mov x2, #0\n" // parent_tidptr
|
||||
"mov x3, #0\n" // tls
|
||||
"mov x4, #0\n" // child_tidptr
|
||||
"svc #0\n" // Make the syscall
|
||||
"mov %0, x0\n" // save return value
|
||||
// Output operands
|
||||
: "=r" (rtn)
|
||||
// Input operand
|
||||
: "r" (flags)
|
||||
// Clobbered registers
|
||||
: "x0", "x1", "x2", "x3", "x4", "x8"
|
||||
);
|
||||
return rtn;
|
||||
}
|
||||
#elif defined(__x86_64__)
|
||||
long int fork() {
|
||||
long int rtn;
|
||||
/*
|
||||
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
|
||||
asm volatile(
|
||||
"movq %%rax, 56\n" // syscall number for 'clone' is 56 for x86-64
|
||||
"movq %%rdi, %1\n" // clone flags
|
||||
"movq %%rsi, 0\n" // child stack pointer
|
||||
"movq %%rdx, 0\n" // parent tidptr
|
||||
"movq %%r10, 0\n" // child tidptr
|
||||
"movq %%r8, 0\n" // tls
|
||||
"syscall\n" // syscall
|
||||
"movq %0, %%rax\n" // save return value
|
||||
: "=r"(rtn)
|
||||
: "r"(flags)
|
||||
: "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "r10", "r11"
|
||||
);
|
||||
*/
|
||||
asm volatile(
|
||||
"syscall\n" // syscall
|
||||
"movq %0, %%rax\n" // save return value
|
||||
: "=r"(rtn)
|
||||
: "a"(57)
|
||||
:
|
||||
);
|
||||
return rtn;
|
||||
}
|
||||
#elif defined(__mips__)
|
||||
long int fork() {
|
||||
int rtn;
|
||||
asm (
|
||||
"li $v0, 4002\n"
|
||||
"syscall\n"
|
||||
"move %0, $v0"
|
||||
:"=r" (rtn)
|
||||
:
|
||||
: "a0", "v0"
|
||||
);
|
||||
return (long int)rtn;
|
||||
}
|
||||
#endif
|
||||
|
||||
void exit(int status) {
|
||||
#if defined(__x86_64__)
|
||||
asm volatile(
|
||||
"syscall"
|
||||
:
|
||||
: "a"(60), "D"(status)
|
||||
:
|
||||
);
|
||||
#elif defined(__aarch64__)
|
||||
asm ( "mov x0, %0\n" // Move the exit status into register x0
|
||||
"mov x8, #93\n" // Syscall number for 'exit' is 93 in AArch64
|
||||
"svc #0\n" // Make the syscall
|
||||
: // No output operands
|
||||
: "r" ((long)status) // Input operand: status
|
||||
: "x0", "x8" // Clobbered registers
|
||||
);
|
||||
#elif defined(__mips__)
|
||||
asm ( "move $a0, %0\n"
|
||||
"li $v0, 4001\n"
|
||||
"syscall"
|
||||
:
|
||||
: "r" (status)
|
||||
: "a0", "v0"
|
||||
);
|
||||
#endif
|
||||
for(;;);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
#if defined(__x86_64__) || defined(__aarch64__) || defined(__mips__)
|
||||
long int pid = fork();
|
||||
char msg[17] = {' '};
|
||||
msg[16] = '\n';
|
||||
for(int i = 0; i < 16; ++i) {
|
||||
char nibble = (pid >> ((15 - i) * 4)) & 0xf;
|
||||
char c;
|
||||
if (nibble > 9) {c = nibble + '7';}
|
||||
else {c = nibble + '0';}
|
||||
msg[i] = c;
|
||||
}
|
||||
print(msg, 17);
|
||||
#else
|
||||
char *msg = "Hello World!\n";
|
||||
print(msg, 13);
|
||||
#endif
|
||||
return 69;
|
||||
}
|
||||
void __libc_start_main() {exit(main());}
|
||||
|
||||
void _start() {
|
||||
__libc_start_main();
|
||||
}
|
||||
|
||||
void __start() {
|
||||
_start();
|
||||
}
|
||||
/*
|
||||
#if defined(__x86_64__)
|
||||
printf("Compiled for x86_64 architecture\n");
|
||||
#elif defined(__i386__)
|
||||
printf("Compiled for x86 architecture\n");
|
||||
#elif defined(__arm__)
|
||||
printf("Compiled for ARM architecture\n");
|
||||
#elif defined(__aarch64__)
|
||||
printf("Compiled for ARM64 architecture\n");
|
||||
#elif defined(__mips__)
|
||||
printf("Compiled for ARM architecture\n");
|
||||
#else
|
||||
printf("Unknown architecture\n");
|
||||
#endif
|
||||
*/
|
||||
|
||||
10
sys.h
10
sys.h
@ -6,15 +6,7 @@ void exit(int8_t status);
|
||||
#define STDIO 1
|
||||
#define STDERR 2
|
||||
intptr_t write(int32_t fd, const void* buf, intptr_t size);
|
||||
#if defined(__x86_64__)
|
||||
intptr_t read(int32_t fd, const void* buf, intptr_t size);
|
||||
#endif
|
||||
//intptr_t read(int32_t fd, const void* buf, intptr_t size);
|
||||
|
||||
uint32_t fork();
|
||||
|
||||
// Provide memset for clang
|
||||
void *memset(void* s, int c, unsigned long n) {
|
||||
int8_t* mem = s;
|
||||
for(long int i = 0; i < n; ++i) mem[i] = c;
|
||||
}
|
||||
#endif // !MINIMALSYS_H
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user