Add fork support for x86_64
This commit is contained in:
parent
80d5606a81
commit
06de3cc17d
38
print.c
38
print.c
@ -30,9 +30,9 @@ long print(const void *buf, long count) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__aarch64__)
|
|
||||||
#define CLONE_CHILD_SETTID 0x1000000
|
#define CLONE_CHILD_SETTID 0x1000000
|
||||||
#define CLONE_CHILD_CLEARTID 0x200000
|
#define CLONE_CHILD_CLEARTID 0x200000
|
||||||
|
#if defined(__aarch64__)
|
||||||
long int fork() {
|
long int fork() {
|
||||||
long int rtn;
|
long int rtn;
|
||||||
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
|
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
|
||||||
@ -42,8 +42,8 @@ long int fork() {
|
|||||||
"mov x0, %1\n" // clone flags
|
"mov x0, %1\n" // clone flags
|
||||||
"mov x1, #0\n" // child stack pointer
|
"mov x1, #0\n" // child stack pointer
|
||||||
"mov x2, #0\n" // parent_tidptr
|
"mov x2, #0\n" // parent_tidptr
|
||||||
"mov x3, #0\n" // child_tidptr
|
"mov x3, #0\n" // tls
|
||||||
"mov x4, #0\n" // tls
|
"mov x4, #0\n" // child_tidptr
|
||||||
"svc #0\n" // Make the syscall
|
"svc #0\n" // Make the syscall
|
||||||
"mov %0, x0\n" // save return value
|
"mov %0, x0\n" // save return value
|
||||||
// Output operands
|
// Output operands
|
||||||
@ -55,6 +55,34 @@ long int fork() {
|
|||||||
);
|
);
|
||||||
return rtn;
|
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;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void exit(int status) {
|
void exit(int status) {
|
||||||
@ -87,7 +115,7 @@ void exit(int status) {
|
|||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
#if defined(__aarch64__)
|
#if defined(__x86_64__) || defined(__aarch64__)
|
||||||
long int pid = fork();
|
long int pid = fork();
|
||||||
char msg[17] = {' '};
|
char msg[17] = {' '};
|
||||||
msg[16] = '\n';
|
msg[16] = '\n';
|
||||||
@ -99,7 +127,7 @@ int main() {
|
|||||||
msg[i] = c;
|
msg[i] = c;
|
||||||
}
|
}
|
||||||
print(msg, 17);
|
print(msg, 17);
|
||||||
#elif
|
#else
|
||||||
char *msg = "Hello World!\n";
|
char *msg = "Hello World!\n";
|
||||||
print(msg, 13);
|
print(msg, 13);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user