오후 11:57 2006-08-26
조경민 bro@shinbiro.com
WinNT EPROCESS의 DirectoryTableBase 배열이 두개?
=====================================================

윈도우의 프로세스를 위한 데이터 구조체인 EPROCESS안에는 다음과 같은 process를 위한 page directory를 위해서
정의된 필드가 있습니다.

EPROCESS
{
   +018 uint32 DirectoryTableBase[2];
}

그런데 이상한 점으로 왜 배열로 2개 요소가 필요한가였는데요,
찾아본 결과 [0] 요소는 해당 프로세스를 위한 우리가 알고 있는 page directory이고요.
[1]은 hyperspace set를 위한 page directory라고 합니다.

새로운 프로세스 공간을 만들기 위해서 사용되는 undocumented 함수중
MmCreateProcessAddressSpace()에 대한 내용입니다.

MmCreateProcessAddressSpace (x86)
Take the WorkingSet lock
Take the PFN lock so we can get physical pages
Allocate the page directory and set into DirectoryTableBase[0]
Allocate the page directory for hyperspace and set into DirectoryTableBase[1]
Allocate pages for the VAD allocation bitmap and the working set list
Release the PFN lock
Initialize the hyperspace map
Under the expansion lock insert the new process onto MM's internal ProcessList
Map the page directory page into hyperspace
Setup the self-map
Fill in the system page directories
Release the WorkingSet lock
Increment the session reference count


hyperspace에 대해서 더 찾아본 결과 아래와 같은 곳에서 힌트를 얻게 되었습니다.
http://www.windowsitlibrary.com/Content/356/04/3.html
hyperspace는 다른 프로세스를 자신 프로세스 공간에 매핑하기 위해서 사용된답니다.
관련함수로 MmMapPageInHyperspace()가 존재하는데요.

callgate매커니즘을 이용하여 showdir.exe라는 프로그램을 두번 실행하여 두 프로세스의
pagedirectory를 출력하였는데, 상위 영역(upper-half)은 다 같을줄 알았는데 두 엔트리가 달랐다.
(callgate매커니즘은 win32 application code가 ring0에서 돌수 있도록 하는 트릭이고요.
윈도우즈에서 AWE활성화하지 않았다면 기본 하위 2G는 애플리케이션 영역이고, 상위 2G는 커널영역으로
모든 프로세스에 대해서 상위 2G는 모두 같은 영역으로 매핑되는데 이 상위를 upper-half를 의미)

이제 관련된 구문을 인용해석하면 아래와 같습니다

두번째 미스테리를 보자. 이 페이지 디렉터리 엔트리에 의한 4MB 영역은 내부적으로 hyperspace로 표현된다.
이 영역은 다른 프로세스들에 속하는 물리 페이지들을 나의 가상 주소 공간으로 매핑하는데 사용된다.
예를 들어, MmMapPageInHyerspace(역시 언다큐먼트)같은 함수는 이 영역안의 가상 주소를 사용한다.
이 영역은 또한 프로세스 생성의 시작 단계에서 사용된다. 예를들어 PROGMAN.EXE같은 부모 프로세스가
NOTEPAD.EXE같은 자식 프로세스를 spawn할때, PROGMAN.EXE는 NOTEPAD.EXE를 위한 주소 공간을
생성해야 한다. 이는 MmCreateProcessAddressSpace() 함수 안에서 이뤄진다. 프로세스가 생성될 때 프로세스를
위한 주소 공간은 반드시 생성되어야 한다. 주소 공간은 페이지 디렉터리일 뿐이다. 또한 페이지 디렉터리의 상위 영역 엔트리는
두개의 엔트리를 제외하고 아까 실험했듯이 모든 프로세스에서 같다. 상위 영역 엔트리들은 프로세스가 spawn되기 위해서
생성되어야 한다. MmCreateProcessAddressSpace()함수는 3개 페이지 메모리를 생성한다. 첫 페이지는 page directory를
위해서 두번째 페이지는 hyperspace 페이지 테이블 엔트리를 세번째 페이지는 프로세스가 spawn되기 위한 working set 정보를
담고 있다.

일단 세 페이지가 할당된 후 MmCreateProcessAddressSpace()는 첫번째 물리페이지를 MmMapPageInHyperSpace()로
매핑한다. MmMapPageInHyperSpace()는 PROGAMAN.EXE의 컨텍스트에서 실행됨을 주시하라. 이제 이 함수는
페이지 디렉터리의 상위 영역안의 페이지 디렉터리 엔트리들을 매핑된 hyperspace 가상 주소로 복사한다. 쉽게 PROGMAN.EXE
이 NOTEPAD.EXE를 위한 페이지 디렉터리를 생성한다.

Let’s now turn to the second mysterious entry. The 4MB area covered by this page directory entry is internally referred to as hyperspace. This area is used for mapping the physical pages belonging to other processes into virtual address space. For example, a function such as MmMapPageInHyperspace() uses the virtual addresses in this range. This area is also used during the early stages of process creation. For example, when a parent process such as PROGMAN.EXE spawns a child process such as NOTEPAD.EXE, PROGMAN.EXE has to create the address space for NOTEPAD.EXE. This is done as a part of the MmCreateProcessAddressSpace() function. For starting any process, an address space must be created for the process. Address space is nothing but page directory. Also, the upper-half entries of page directory are common for all processes except for the two entries that we have already discussed. These entries need to be created for the process being spawned. The MmCreateProcessAddressSpace() function allocates three pages of memory: the first page for the page directory, the second page for holding the hyperspace page table entries, and the third page for holding the working set information for the process being spawned.

Once these pages are allocated, the function maps the first physical page in the address space using the MmMapPageInHyperSpace() function. Note that the MmMapPageInHyperSpace() function runs in the context of PROGMAN.EXE. Now the function copies the page directory entries in the upper half of the page directory to the mapped hyperspace virtual address. In short, PROGMAN.EXE creates the page directory for the NOTEPAD.EXE

'KB > Win32/x86' 카테고리의 다른 글

EPROCESS ProcessLock 관련  (0) 2006.08.27
workingset size  (0) 2006.08.27
Lock Free or Wait Free  (0) 2006.08.21
Windows Kernel Debugger  (0) 2006.08.10
TLR (Transactional Lock Removal)  (0) 2006.07.29

+ Recent posts