single order page table 과 2-order page table

Cute_Security15·2024년 6월 11일
0

커널

목록 보기
10/10

상황

32bit os 에서 프로그램은 가상주소공간 4GB 를 제공받는데,
이 가상주소공간이 실제 물리주소로 변환되려면 page table 을 사용하게 된다.

page table 에는 물리메모리 (page frame) 주소가 들어있고,

가상주소를 page 크기(4k) 로 나눈 후,
몫을 page table index, 나머지를 물리메모리 내 offset 으로 사용하는 방식을
single order page table 이라고 부른다.

  • 상위 20bit 는 page table index
  • 하위 12bit 는 page 내 offset

이 방식이 계산하기 간단해 보이는데, 실제로는 page table 을 여러개 두고있다.
왜 굳이 그렇게 할까?

2-order 로 계산

먼저 2-order 할 경우는 다음과 같을 것이다

책에서는 2-order 를 통해, page table entry 갯수를 줄일수 있다고 설명하고 있다.
하지만, 결국 어떻게 됬든 bit 갯수는 동일하게 쓰는것 아닌가 라는 생각이 들었다.

좀더 구체화 시켜보면

   0   0    1   0    0   0    0   0
00000000 00010000 00000000 00000000

//// single order page table
........ ........ ....              : page table index [20]
                      .... ........ : page 내 offset   [12]

//// 2-order page table
........ ..                         : first page table index      [10]
           ...... ....              : secondary page table index  [10]
                      .... ........ : page 내 offset              [12]

이때, bit 갯수가 동일하면 entry 갯수도 결국은 동일하게 된다.

  • 2^10 x 2^10 = 2^20

single page table entry 의 갯수는 2^20 : 1048576 개
2-order page table entry 의 갯수는 2^10 x 2^10 : 1048576 개

multi-level page table 의 2가지 장점

구글링 해본결과, 듀크대 pdf 에서 답을 찾을수 있었다.
https://people.duke.edu/~tkb13/courses/ece250-2020su/resources/umass-trekp-csc262-lecture8-virtualmemory.pdf

A multi-level page table has several advantages: 
1. Saves Space: A secondary page table only needs to be allocated if there are any valid mappings in that address range. 
Therefore, only the main page table “wastes” space on those addresses. 

2. Swappable Page Tables: Because the main page table now acts as the entrypoint for address translation, 
secondary page tables can be swapped out to save space. Of course, this makes address translation more complicated 
as secondary page tables may have to be swapped in before the translation can complete.

처음 그림에서, single order page table 은 가상-물리주소 mapping 유무와 상관없이
물리 메모리를 차지하게 되는데, 이 메모리는 32bit os 기준으로 1048576 * 4 Bytes

즉 프로세스 당 4MB 를 page table 로 사용하고 있다. (90년대 기준으로 $200 의 손해)

  • 4K page 기준으로, 256개 page frame 이 무의미하게 소비

왜 무의미하게 소비해야만 할까?
--> page table 에 값이 없을 경우, OS 의 page fault handler 가 page 를 받아오는데,
이 값을 저장할 곳이 있어야 하기 때문
이다.

그럼 2-order 로 구성하면 어떻게 될까?
--> 2-order 로 구성할 경우, main page table 만 물리 메모리를 차지하고,
secondary page table 는 가상-물리주소 mapping 가 있는 주소 범위에만 생성되므로
물리 메모리를 유의미하게 사용할수 있다.

  • (main page table 이 secondary page table 이 없는 주소 범위에 접근할때,
    그제서야 secondary page table 을 생성)

따라서 32bit os 기준으로 2^10 (1024) 개 entry,
프로세스 당 '초기' page table 메모리로 4KB 만 사용하면 되므로
메모리 자원을 크게 아낄수 있게 된다.

또한, secondary page table 자체도 더이상 해당 가상-물리주소 mapping 이 안쓰인다고 판단되면, swap out 시켜 추가로 물리 메모리를 확보할수 있으므로,
OS 가 좀더 유연하게 mapping 정보를 관리할수 있게 된다.

profile
관심분야 : Filesystem, Data structure, user/kernel IPC

0개의 댓글