blob: 6b0d0d2a98dc3457e1fe602d887ecf49a7378ccd [file] [log] [blame] [raw]
/* A part of the Native C Library for Windows NT
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <windows.h>
#include <nt.h>
#include <errno.h>
#include <unistd.h>
#include <process.h>
//#include <stdio.h>
static int is_child = 0;
pid_t fork() {
if(is_child) {
is_child = 0;
return 0;
}
void *ph, *th;
OBJECT_ATTRIBUTES object_attrib = { sizeof(OBJECT_ATTRIBUTES), NULL, NULL, 0, NULL, NULL };
long int status = NtCreateProcess(&ph, PROCESS_ALL_ACCESS, &object_attrib, (void *)-1, 1, NULL, NULL, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
CONTEXT context = { .ContextFlags = CONTEXT_FULL | CONTEXT_FLOATING_POINT };
status = NtGetContextThread((void *)-2, &context);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) {
NtClose(ph);
__set_errno_from_ntstatus(status);
return -1;
}
#ifdef __i386__
__asm__(" movl %%gs, %0\n" : "=r"(context.SegGs));
__asm__(" movl %%fs, %0\n" : "=r"(context.SegFs));
__asm__(" movl %%es, %0\n" : "=r"(context.SegEs));
__asm__(" movl %%ds, %0\n" : "=r"(context.SegDs));
__asm__(" movl %%edi, %0\n" : "=r"(context.Edi));
__asm__(" movl %%esi, %0\n" : "=r"(context.Esi));
__asm__(" movl %%ebx, %0\n" : "=r"(context.Ebx));
__asm__(" movl %%edx, %0\n" : "=r"(context.Edx));
__asm__(" movl %%ecx, %0\n" : "=r"(context.Ecx));
__asm__(" movl %%eax, %0\n" : "=r"(context.Eax));
__asm__(" movl %%ebp, %0\n" : "=r"(context.Ebp));
__asm__(" movl %%cs, %0\n" : "=r"(context.SegCs));
__asm__(" movl %%esp, %0\n" : "=r"(context.Esp));
__asm__(" movl %%ss, %0\n" : "=r"(context.SegSs));
#if 0
printf("nativelibc debug:\n"
"Dr0=%lx, Dr1=%lx, Dr2=%lx, Dr3=%lx, Dr6=%lx, Dr7=%lx,\n"
"SegGs = %lx, SegFs = %lx, SegEs = %lx, SegDs = %lx,\n"
"Edi=%lx, Esi=%lx, Ebx=%lx, Edx=%lx, Ecx=%lx, Eax=%lx, Ebp=%lx, Eip=%lx,\n"
"SegCs = %lx, EFlags = %lx, Esp=%lx, SegSs = %lx\n",
context.Dr0,
context.Dr1,
context.Dr2,
context.Dr3,
context.Dr6,
context.Dr7,
context.SegGs,
context.SegFs,
context.SegEs,
context.SegDs,
context.Edi,
context.Esi,
context.Ebx,
context.Edx,
context.Ecx,
context.Eax,
context.Ebp,
context.Eip,
context.SegCs,
context.EFlags,
context.Esp,
context.SegSs);
#endif
context.Eip = (unsigned long int)fork + 31;
//printf("fork = %p, context.Eip = 0x%lx\n", fork, context.Eip);
MEMORY_BASIC_INFORMATION mbi;
status = NtQueryVirtualMemory((void *)-1, (void *)context.Esp, MemoryBasicInformation, &mbi, sizeof mbi, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) {
NtClose(ph);
__set_errno_from_ntstatus(status);
return -1;
}
#else
#error "Not supported arch"
#endif
INITIAL_TEB stack = { 0, 0, (char *)mbi.BaseAddress + mbi.RegionSize, mbi.BaseAddress, mbi.AllocationBase };
CLIENT_ID cid;
status = NtCreateThread(&th, THREAD_ALL_ACCESS, &object_attrib, ph, &cid, &context, &stack, 1);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) {
NtClose(ph);
__set_errno_from_ntstatus(status);
return -1;
}
THREAD_BASIC_INFORMATION tbi;
status = NtQueryInformationThread((void *)-2, ThreadBasicInformation, &tbi, sizeof tbi, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) goto failed;
NT_TIB *tib = &tbi.TebBaseAddress->Tib;
status = NtQueryInformationThread(th, ThreadBasicInformation, &tbi, sizeof tbi, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) goto failed;
status = NtWriteVirtualMemory(ph, tbi.TebBaseAddress, &tib->ExceptionList, sizeof tib->ExceptionList, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
if(status < 0) goto failed;
status = NtResumeThread(th, NULL);
//printf("nativelibc debug: fork: status = 0x%lx\n", status);
NtClose(th);
if(status < 0) {
NtClose(ph);
__set_errno_from_ntstatus(status);
return -1;
}
__child_process_table_add(cid.UniqueProcess, ph);
return cid.UniqueProcess;
failed:
NtClose(ph);
NtClose(th);
__set_errno_from_ntstatus(status);
return -1;
}