📄 process.c
字号:
/* * process.c * * Copyright (C) 2006 Insigme Co., Ltd * * Authors: * - Chenzhan Hu, Limin Jin * * This software has been developed while working on the Linux Unified Kernel * project (http://linux.insigma.com.cn) in the Insigma Reaserch Institute, * which is a subdivision of Insigma Co., Ltd (http://www.insigma.com.cn). * * The project is sponsored by Insigma Co., Ltd. * * The authors can be reached at linux@insigma.com.cn. * * 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. * * Revision History: * Jan 2006 - Created. *//* * process.c: * Reference to Reactos Kernel code */#include <linux/win32_process.h>#include <linux/mman.h>#include "../win32.h"#include "../section.h"#include "../process.h"#include "../thread.h"#include "../pefile.h"#include "virtual.h"#include "attach.h"#ifdef CONFIG_UNIFIED_KERNEL//#define ktrace(fmt ...)//#define kdebug(fmt ...)extern unsigned long MmUserProbeAddress;static unsigned long extra_page = 0;#define USER_SHARED_DATA (0x7FFE0000)#define MIN(a, b) ((a) > (b) ? (b) : (a))/* * MiCreatePebOrTeb * called for alloc peb or teb */ PVOIDSTDCALLMiCreatePebOrTeb(PEPROCESS Process, PVOID BaseAddress) //tebbase{ void *alloc_addr; NTSTATUS status; LARGE_INTEGER multi = { .QuadPart = 0LL }; ktrace("MiCreatePebOrTeb\n"); alloc_addr = BaseAddress; do { status = MmCreateMemoryArea(Process, NULL, 0, &alloc_addr, PAGE_SIZE, _PAGE_READWRITE, NULL, TRUE, FALSE, multi); alloc_addr -= PAGE_SIZE; } while (alloc_addr >= 0 && status != STATUS_SUCCESS); if (STATUS_SUCCESS == status) alloc_addr += PAGE_SIZE; if ((STATUS_SUCCESS != status) || (PEB_BASE == (unsigned long )BaseAddress && alloc_addr != BaseAddress)) { kdebug("***error create peb or teb\n"); return NULL; } ktrace("end MiCreatePebOrTeb created 0x%lx\n", alloc_addr); return alloc_addr;} /* end MiCreatePebOrTeb *//* * MmCreatePeb * called for create peb */NTSTATUSSTDCALLMmCreatePeb(PEPROCESS Process){ struct ethread *thread; BOOL attached = false; PPEB peb = NULL; struct mm_struct *mm = NULL; ktrace("MmCreatePeb\n"); /* Attach to Process */ if (!(thread = thread_find())) { kdebug("***error find thread\n"); return -EINVAL; } if (thread->threads_process != Process) { mm = KeAttachProcess(&Process->pcb); attached = true; } /* Allocate the PEB */ peb = MiCreatePebOrTeb(Process, (PVOID)PEB_BASE); if (!peb) return -1; /* Initialize the PEB */ memset(peb, 0, sizeof(PEB)); /* Set up data */ peb->ImageBaseAddress = Process->section_base_address; peb->OSMajorVersion = 5; peb->OSMinorVersion = 0; peb->OSBuildNumber = 13; peb->OSPlatformId = 2; /* VER_PLATFORM_WIN32_NT */ peb->OSCSDVersion = 0; peb->AnsiCodePageData = 0; /* FIXME */ peb->OemCodePageData = 0; /* FIXME */ peb->UnicodeCaseTableData = 0; /* FIXME */ peb->NumberOfProcessors = 1; peb->BeingDebugged = (BOOLEAN)(Process->debug_port ? true : false); Process->peb = peb; if (attached) KeDetachProcess(mm); ktrace("end MmCreatePeb: Peb created at %p\n", peb); return STATUS_SUCCESS;} /* end MmCreatePeb *//* * MmCreateTeb * called for create teb */PTEBSTDCALLMmCreateTeb(PEPROCESS Process, PCLIENT_ID ClientId, PINITIAL_TEB InitialTeb){ struct ethread *thread; BOOL attached = false; PTEB teb; struct mm_struct *mm = NULL; if (!(thread = thread_find())) { kdebug("***error find thread\n"); return NULL; } if (thread->threads_process != Process) { mm = KeAttachProcess(&Process->pcb); attached = true; } /* Allocate the TEB */ if (!(teb = MiCreatePebOrTeb(Process, (void *)TEB_BASE))){ kdebug("***create teb error\n"); return NULL; } memset(teb, 0, sizeof(TEB)); /* Set TIB Data */ teb->Tib.ExceptionList = (PVOID)0xFFFFFFFF; teb->Tib.DUMMYUNIONNAME.Version = 1; teb->Tib.Self = (PNT_TIB)teb; /* Set TEB Data */ teb->Cid = *ClientId; teb->RealClientId = *ClientId; teb->Peb = Process->peb; teb->CurrentLocale = 0; /* FIXME: PsDefaultThreadLocaleId; */ if (attached) KeDetachProcess(mm); ktrace("end MmCreateTeb: Teb created at %p\n", teb); return teb; } /* end MmCreateTeb *//* * MmCreateProcessAddressSpace */NTSTATUSSTDCALLMmCreateProcessAddressSpace(struct eprocess *process, struct win32_section *section){ NTSTATUS status = STATUS_SUCCESS; ULONG ViewSize = 0; PVOID ImageBase = 0; unsigned long start_code = 0, start_data = 0, end_code = 0, end_data = 0; unsigned long pe_brk = 0; struct win32_image_section *wis; struct mm_struct *mm; struct task_struct *tsk; ktrace("MmCreateProcessAddressSpace\n"); tsk = get_first_thread(process)->et_task; mm = tsk->mm; /* Check if there's a Section Object */ if (section) { UCHAR *name, *p; int len; status = MmMapViewOfSection(section->ws_obj, process, (PVOID*)&ImageBase, 0, 0, NULL, (PSIZE_T)&ViewSize, 0, _MEM_COMMIT, _PAGE_READWRITE); if (status != STATUS_SUCCESS) { kdebug("Failed to map process Image\n"); return status; } for (wis = section->ws_sections; wis < section->ws_sections + section->ws_nsecs; wis++) { unsigned long k; if (wis->wis_character & IMAGE_SCN_TYPE_NOLOAD) continue; k = section->ws_realbase + wis->wis_rva; /* * Check to see if the section's size will overflow the * allowed task size. Note that p_filesz must always be * <= p_memsz so it is only necessary to check p_memsz. */ if (k > TASK_SIZE || TASK_SIZE - wis->wis_size < k) /* Avoid overflows. */ goto out; if (wis->wis_character & IMAGE_SCN_MEM_EXECUTE) { start_code = k; end_code = k + wis->wis_rawsize; } else { if (!start_data) start_data = k; end_data = k + wis->wis_rawsize; } k += wis->wis_size; if (pe_brk < k) /* pe_brk used set mm->brk */ pe_brk = k; /* TODO: start_data and end_data, diff to ELF */ } mm->brk = pe_brk; mm->start_code = start_code; mm->start_data = start_data; mm->end_code = end_code; mm->end_data = end_data; /* extra page, used for interpreter ld-linux.so */ down_write(&mm->mmap_sem); if ((extra_page = win32_do_mmap_pgoff(tsk, 0, NULL, pe_brk, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0)) != pe_brk) { up_write(&mm->mmap_sem); goto out; } up_write(&mm->mmap_sem); mm->brk = pe_brk + PAGE_SIZE; process->spare0[0] = (void *)extra_page; section->ws_entrypoint += section->ws_realbase; /* Save the pointer */ process->section_base_address = ImageBase; /* Determine the image file name and save it to EPROCESS */ name = section->ws_wfile->wf_control->wfc_myself->o_name.name; p = strrchr(name, '\\'); if (p) p++; else p = name; len = strlen(p); len = MIN(len, sizeof(process->image_file_name)); memcpy(process->image_file_name, p, len); task_lock(tsk); strlcpy(tsk->comm, p, sizeof(tsk->comm)); task_unlock(tsk); }out: return status;} /* end MmCreateProcessAddressSpace */#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -