Notice
Recent Posts
Recent Comments
Link
Β«   2025/10   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

JunHyeok

[PintOS - VM] Introduction λ³Έλ¬Έ

PintOS

[PintOS - VM] Introduction

junhyeok-log 2024. 6. 3. 13:34

🎬 Introduction

By now you should have some familiarity with the inner workings of Pintos. Your OS can properly handle multiple threads of execution with proper synchronization, and can load multiple user programs at once. However, the number and size of programs that can run is limited by the machine's main memory size. In this assignment, you will remove that limitation by buiding an illusion of infinite memory.

Thread λ‹¨μ›μ—μ„œ μš°λ¦¬λŠ” μ—¬λŸ¬ μ“°λ ˆλ“œλ₯Ό 닀루며 동기화 ν•˜λŠ” 법을 ν–ˆμ—ˆμ£ ?
μ΄λ²ˆμ—λŠ” μ—¬λŸ¬κ°œμ˜ userprog을 λ‘œλ“œν•¨μ€ λ¬Όλ‘ , Main Memory μ‚¬μ΄μ¦ˆμ˜ μ œμ•½μ„ λ²—μ–΄λ‚˜ 각각의 ν”„λ‘œμ„ΈμŠ€κ°€ Main Memory 전체λ₯Ό ν™œμš©ν•œλ‹€λŠ” ν™˜μƒμ„ 심어쀄 것 μž…λ‹ˆλ‹€!

πŸ›ŽοΈ Background

Source Files

You will work in the vm directory for this project. The Makefile is updated to turn on the setting -DVM. We provide an enormous amount of template code. You MUST follow the given template. That is, if you submit the code, that is not based on the given template, you get 0pts. Also, you should never change the template where it is marked "DO NOT CHANGE". Here, we provide some details about each template file that you will be modifying.

λ°˜λ“œμ‹œ μ£Όμ–΄μ§„ ν…œν”Œλ¦Ώμ„ λ”°λ₯΄μ„Έμš”!! μ£Όμ–΄μ§„ ν…œν”Œλ¦Ώμ— κΈ°λ°˜ν•˜μ§€ μ•Šκ³  μ½”λ“œλ₯Ό μ œμΆœν•œλ‹€λ©΄ 0점을 λ°›μŠ΅λ‹ˆλ‹€! λ˜ν•œ, β€œDO NOT CHANGE”라고 μ ν˜€μžˆλŠ” λΆ€λΆ„μ˜ μ½”λ“œλŠ” μ ˆλŒ€ μˆ˜μ •ν•˜μ§€ λ§ˆμ„Έμš”. μ•„λž˜μ— 각 ν…œν”Œλ¦Ώ νŒŒμΌμ—μ„œ 당신이 μˆ˜μ •ν•˜κ²Œ 될 뢀뢄에 λŒ€ν•œ μžμ„Έν•œ μ„€λͺ…을 ν•˜κ² μŠ΅λ‹ˆλ‹€.

ν•˜μ§€ 말라면 ν•˜μ§€ 말자!

include/vm/vm.h, vm/vm.c

enum vm_type {
    /* page not initialized */
    VM_UNINIT = 0,
    /* page not related to the file, aka anonymous page */
    VM_ANON = 1,
    /* page that realated to the file */
    VM_FILE = 2,
    /* page that hold the page cache, for project 4 */
    VM_PAGE_CACHE = 3,

    /* Bit flags to store state */

    /* Auxillary bit flag marker for store information. You can add more
     * markers, until the value is fit in the int. */
    VM_MARKER_0 = (1 << 3),
    VM_MARKER_1 = (1 << 4),

    /* DO NOT EXCEED THIS VALUE. */
    VM_MARKER_END = (1 << 31),
};

VM_UNINIT.c

Provides operations for uninitialized pages (vm_type = VM_UNINIT). Under the current design, all pages are initially set up as uninitialized pages, then it transforms to anonymous pages or file-backed pages.

μ΄ˆκΈ°ν™”λ˜μ§€ μ•Šμ€ νŽ˜μ΄μ§€μ— λŒ€ν•œ 연산을 μ œκ³΅ν•¨. λͺ¨λ“  νŽ˜μ΄μ§€λŠ” VM_UNINIT 둜 μ„€μ •λœ λ‹€μŒVM_ANON ν˜Ήμ€ file-backed pages 둜 λ³€ν™˜λ¨.

vm/anon.c

Provides operations for anonymous pages (vm_type = VM_ANON).

vm/file.c

Provides operations for file-backed pages (vm_type = VM_FILE).

vm/inspect.c

grading을 μœ„ν•œ λ©”λͺ¨λ¦¬ κΈ°λŠ₯ 검사. μˆ˜μ • κΈˆμ§€! 🚫

Memory Terminology

Pages

  • 좜처 : [1]
63          48 47            39 38            30 29            21 20         12 11         0
+-------------+----------------+----------------+----------------+-------------+------------+
| Sign Extend |    Page-Map    | Page-Directory | Page-directory |  Page-Table |    Page    |
|             | Level-4 Offset |    Pointer     |     Offset     |   Offset    |   Offset   |
+-------------+----------------+----------------+----------------+-------------+------------+
              |                |                |                |             |            |
              +------- 9 ------+------- 9 ------+------- 9 ------+----- 9 -----+---- 12 ----+
                                          Virtual Address

A page, sometimes called a virtual page, is a continuous region of virtual memory of size 4,096 bytes (the page size) in length. A page must be page-aligned, that is, start on a virtual address evenly divisible by the page size. Thus, the last 12 bits of a 64-bit virtual address is the page offset (or just offset). The upper bits are used to indicate the index in the page table, which will be introduced soon. With 64-bit system, we use 4-level page table, which makes a virtual address to look like this:

가상 νŽ˜μ΄μ§€λŠ” 크기가 4,096 bytes 인 가상 λ©”λͺ¨λ¦¬μ˜ 연속적인 μ˜μ—­μž…λ‹ˆλ‹€. νŽ˜μ΄μ§€λŠ” νŽ˜μ΄μ§€ 정렬이 λ˜μ–΄ μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€, 즉 νŽ˜μ΄μ§€ 크기둜 κ· λ“±ν•˜κ²Œ λ‚˜λˆŒ 수 μžˆλŠ” 가상 μ£Όμ†Œμ—μ„œ μ‹œμž‘ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ”°λΌμ„œ 64λΉ„νŠΈ 가상 μ£Όμ†Œμ˜ λ§ˆμ§€λ§‰ 12λΉ„νŠΈκ°€ νŽ˜μ΄μ§€ μ˜€ν”„μ…‹(λ˜λŠ” κ·Έλƒ₯ μ˜€ν”„μ…‹)μž…λ‹ˆλ‹€. μœ„μͺ½ λΉ„νŠΈλŠ” νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ˜ 인덱슀λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 데 μ‚¬μš©λ˜λ©°, μ΄λŠ” κ³§ μ†Œκ°œν•  νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ— ν‘œμ‹œλ©λ‹ˆλ‹€. 64λΉ„νŠΈ μ‹œμŠ€ν…œμ—μ„œλŠ” 가상 μ£Όμ†Œλ₯Ό λ‹€μŒκ³Ό 같이 보이도둝 ν•˜λŠ” 4단계 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ„ μ‚¬μš©ν•©λ‹ˆλ‹€:

4KBλŠ” 4096 λ°”μ΄νŠΈμž…λ‹ˆλ‹€.
4096은 212
λ”°λΌμ„œ, 4KB νŽ˜μ΄μ§€μ˜ μ£Όμ†Œλ₯Ό λ‚˜νƒ€λ‚΄λ €λ©΄ 12λΉ„νŠΈκ°€ ν•„μš”ν•©λ‹ˆλ‹€.

Each process has an independent set of user (virtual) pages, which are those pages below the virtual address KERN_BASE (0x8004000000). The set of kernel (virtual) pages, on the other hand, is global, and thus remain in the same position regardless of what thread or process is running. The kernel may access both user and kernel pages, but a user process may access only its own user pages. See Virtual Memory Layout, for more information.

각 ν”„λ‘œμ„ΈμŠ€λŠ” KERN_BASE(0x8004000000) 'μ•„λž˜' 에 독립적인 곡간을 κ°–λŠ” user (virtual) pagesλ₯Ό κ°–κ³  μžˆμŠ΅λ‹ˆλ‹€. λ°˜λ©΄μ—, kernel (virtual) pages λŠ” μ „μ—­μ μœΌλ‘œ 쓰이며, 항상 같은 μœ„μΉ˜μ— μ‘΄μž¬ν•©λ‹ˆλ‹€.

컀널은 μœ μ € νŽ˜μ΄μ§€μ™€ 컀널 νŽ˜μ΄μ§€ λͺ¨λ‘μ— μ ‘κ·Όν•  수 μžˆμ§€λ§Œ, μœ μ € ν”„λ‘œμ„ΈμŠ€λŠ” 본인의 μœ μ € νŽ˜μ΄μ§€μ—λ§Œ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€

KERN_BASE (0x8004000000) πŸ€”?

이 가상 μ£Όμ†ŒλŠ” 컀널 곡간이 μ‹œμž‘λ˜λŠ” 지점을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 즉, 이 μ£Όμ†Œ μ΄μƒμ˜ μ˜μ—­μ€ 컀널 μ „μš©μ΄κ³ , 이 μ£Όμ†Œ 미만의 μ˜μ—­μ€ μ‚¬μš©μž μ „μš©μž…λ‹ˆλ‹€.

Pintos provides several useful functions for working with virtual addresses. See Section Virtual Addresses, for details.
https://casys-kaist.github.io/pintos-kaist/appendix/virtual_address.html

Frames

A frame, sometimes called a physical frame or a page frame, is a continuous region of physical memory. Like pages, frames must be page-size and page-aligned. Thus, a 64-bit physical address can be divided into a frame number and a frame offset (or just offset), like this:

Frame 은 물리적 ν”„λ ˆμž„ λ˜λŠ” page frame 으둜 λΆˆλ¦¬κΈ°λ„ ν•˜λ©°, 물리적 λ©”λͺ¨λ¦¬μ˜ 연속적인 μ˜μ—­μž…λ‹ˆλ‹€. νŽ˜μ΄μ§€μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ ν”„λ ˆμž„μ€ page-size와 page-aligned이 λ˜μ–΄ μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. λ”°λΌμ„œ 64λΉ„νŠΈ 물리적 μ£Όμ†ŒλŠ” λ‹€μŒκ³Ό 같이 ν”„λ ˆμž„ λ²ˆν˜Έμ™€ ν”„λ ˆμž„ μ˜€ν”„μ…‹(λ˜λŠ” κ·Έλƒ₯ μ˜€ν”„μ…‹)으둜 λ‚˜λˆŒ 수 μžˆμŠ΅λ‹ˆλ‹€:

                          12 11         0
    +-----------------------+-----------+
    |      Frame Number     |   Offset  |
    +-----------------------+-----------+
              Physical Address

νŽ˜μ΄μ§€ μ •λ ¬ (page-aligned)은 λ©”λͺ¨λ¦¬ μ£Όμ†Œκ°€ νŽ˜μ΄μ§€ 크기의 배수인 경우λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. 즉, νŽ˜μ΄μ§€μ˜ μ‹œμž‘ μ£Όμ†Œκ°€ νŽ˜μ΄μ§€ 크기의 λ°°μˆ˜μ—¬μ•Ό ν•©λ‹ˆλ‹€.
4KB νŽ˜μ΄μ§€μ˜ 경우, νŽ˜μ΄μ§€ μ •λ ¬ μ£Όμ†ŒλŠ” 0x0000, 0x1000, 0x2000 λ“±κ³Ό 같이 4096(0x1000)의 λ°°μˆ˜μž…λ‹ˆλ‹€.

x86-64 doesn't provide any way to directly access memory at a physical address. Pintos works around this by mapping kernel virtual memory directly to physical memory - the first page of kernel virtual memory is mapped to the first frame of physical memory, the second page to the second frame, and so on. Thus, frames can be accessed through kernel virtual memory.

x86-64λŠ” 물리적 μ£Όμ†Œμ—μ„œ λ©”λͺ¨λ¦¬μ— 직접 μ•‘μ„ΈμŠ€ν•˜λŠ” 방법을 μ œκ³΅ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•€ν† μŠ€λŠ” 컀널 가상 λ©”λͺ¨λ¦¬λ₯Ό 물리적 λ©”λͺ¨λ¦¬μ— 직접 λ§€ν•‘ν•˜μ—¬ 이 문제λ₯Ό ν•΄κ²°ν•©λ‹ˆλ‹€. 컀널 가상 λ©”λͺ¨λ¦¬μ˜ 첫 번째 νŽ˜μ΄μ§€λŠ” 물리적 λ©”λͺ¨λ¦¬μ˜ 첫 번째 ν”„λ ˆμž„μ— λ§€ν•‘λ˜κ³ , 두 번째 νŽ˜μ΄μ§€λŠ” 두 번째 ν”„λ ˆμž„μ— λ§€ν•‘λ©λ‹ˆλ‹€. λ”°λΌμ„œ ν”„λ ˆμž„μ€ 컀널 가상 λ©”λͺ¨λ¦¬λ₯Ό 톡해 μ•‘μ„ΈμŠ€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Page Tables

A page table is a data structure that the CPU uses to translate a virtual address to a physical address, that is, from a page to a frame. The page table format is dictated by the x86-64 architecture. Pintos provides page table management code in threads/mmu.c.

νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ€ CPUκ°€ 가상 μ£Όμ†Œλ₯Ό 물리 μ£Όμ†Œ, 즉 pageμ—μ„œ frame으둜 λ³€ν™˜ν•˜λŠ” 데 μ‚¬μš©ν•˜λŠ” 자료ꡬ쑰 μž…λ‹ˆλ‹€. νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ν˜•μ‹μ€ x86-64 μ•„ν‚€ν…μ²˜μ— μ˜ν•΄ κ²°μ •λ©λ‹ˆλ‹€. ν•€ν† μŠ€λŠ” νŽ˜μ΄μ§€ ν…Œμ΄λΈ” 관리 μ½”λ“œλ₯Ό μŠ€λ ˆλ“œ/mmu.c둜 μ œκ³΅ν•©λ‹ˆλ‹€.

The diagram below illustrates the relationship between pages and frames. The virtual address, on the left, consists of a page number and an offset. The page table translates the page number into a frame number, which is combined with the unmodified offset to obtain the physical address, on the right.

μ•„λž˜ λ‹€μ΄μ–΄κ·Έλž¨μ€ page와 frame의 관계λ₯Ό λ³΄μ—¬μ€λ‹ˆλ‹€. μ™Όμͺ½μ˜ 가상 μ£Όμ†ŒλŠ” νŽ˜μ΄μ§€ λ²ˆν˜Έμ™€ μ˜€ν”„μ…‹μœΌλ‘œ κ΅¬μ„±λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ€ νŽ˜μ΄μ§€ 번호λ₯Ό ν”„λ ˆμž„ 번호둜 λ³€ν™˜ν•˜κ³  였λ₯Έμͺ½μ˜ μˆ˜μ •λ˜μ§€ μ•Šμ€ μ˜€ν”„μ…‹κ³Ό κ²°ν•©ν•˜μ—¬ 물리적 μ£Όμ†Œλ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

Swap Slots

A swap slot is a page-size region of disk space in the swap partition. Although hardware limitations dictating the placement of slots are more flexible than for frames, swap slots should be page-aligned because there is no downside in doing so.

μŠ€μ™‘ μŠ¬λ‘―μ€ μŠ€μ™‘ νŒŒν‹°μ…˜μ—μ„œ λ””μŠ€ν¬ κ³΅κ°„μ˜ νŽ˜μ΄μ§€ 크기 μ˜μ—­μž…λ‹ˆλ‹€. 슬둯의 배치λ₯Ό μ§€μ‹œν•˜λŠ” ν•˜λ“œμ›¨μ–΄ μ œν•œμ΄ ν”„λ ˆμž„λ³΄λ‹€ 더 μœ μ—°ν•˜μ§€λ§Œ, μŠ€μ™‘ μŠ¬λ‘―μ€ page-aligned λ₯Ό ν•˜λ˜ μ•ˆν•˜λ˜ 큰 차이가 없기에 (단점은 없기에) μ •λ ¬ν•΄μ€λ‹ˆλ‹€.

πŸ•ΉοΈ Resource Management Overview

You will need to design/implement the following data structures:

Supplemental page table

Enables page fault handling by supplementing the page table. See Managing the Supplemental Page Table below.

page table 을 λ³΄μ‘°ν•˜μ—¬ page fault handling 처리λ₯Ό ν™œμ„±ν™”ν•©λ‹ˆλ‹€. μ•„λž˜ 보좩 νŽ˜μ΄μ§€ ν…Œμ΄λΈ” 관리λ₯Ό μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

Frame table

Allows efficient implementation of eviction policy of physical frames. See Managing the Frame Table below.

물리적 ν”„λ ˆμž„μ˜ 퇴거 정책을 효율적으둜 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜μ˜ ν”„λ ˆμž„ ν…Œμ΄λΈ” 관리λ₯Ό μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

Swap table

Tracks usage of swap slots. See Managing the Swap Table below.

μŠ€μ™‘ 슬둯이 μ‚¬μš©λ˜λŠ” 것을 μΆ”μ ν•©λ‹ˆλ‹€. μ•„λž˜μ˜ μŠ€μ™‘ ν…Œμ΄λΈ” 관리 뢀뢄을 μ°Έκ³ ν•˜μ„Έμš”.

You do not necessarily need to implement three completely distinct data structures: it may be convenient to wholly or partially merge related resources into a unified data structure.

For each data structure, you need to determine what information each element should contain. You also need to decide on the data structure's scope, either local (per-process) or global (applying to the whole system), and how many instances are required within its scope.

To simplify your design, you may store these data structures in non-pageable memory (e.g., memory allocated by calloc or malloc). That means that you can be sure that pointers among them will remain valid.

각 자료ꡬ쑰λ₯Ό μ™„μ „νžˆ λ³„κ°œλ‘œ κ΅¬ν˜„ν•  ν•„μš”λŠ” μ—†μŠ΅λ‹ˆλ‹€. μ„œλ‘œ κ΄€λ ¨λœ μžμ›μ„ ν†΅ν•©λœ 자료ꡬ쑰둜 ν•©μΉ˜λŠ” 것이 νŽΈλ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 각 μžλ£Œκ΅¬μ‘°μ—μ„œ 각 μ›μ†Œκ°€ μ–΄λ–€ 정보λ₯Ό λ‹΄λŠ”μ§€ κ²°μ •ν•΄μ•Ό ν•˜κ³ , 자료ꡬ쑰λ₯Ό μ§€μ—­(ν”„λ‘œμ„ΈμŠ€λ³„) λ˜λŠ” μ „μ—­(전체 μ‹œμŠ€ν…œμ— 적용)으둜 μ μš©ν• μ§€ κ²°μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ˜ν•œ ν•„μš”ν•œ μΈμŠ€ν„΄μŠ€μ˜ 수λ₯Ό κ²°μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.

섀계λ₯Ό λ‹¨μˆœν™”ν•˜κΈ° μœ„ν•΄, non-pageable λ©”λͺ¨λ¦¬μ— μ΄λŸ¬ν•œ 자료ꡬ쑰λ₯Ό μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” callocμ΄λ‚˜ malloc으둜 ν• λ‹Ήλœ λ©”λͺ¨λ¦¬λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. 즉, μžλ£Œκ΅¬μ‘°λ“€ μ‚¬μ΄μ˜ 포인터가 항상 μœ νš¨ν•œ μƒνƒœλ‘œ μœ μ§€λ  것을 보μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Choices of implementation (Performance perspective)

Possible choices for implementation include arrays, lists, bitmaps, and hash tables. An array is often the simplest approach, but a sparsely populated array wastes memory. Lists are also simple, but traversing a long list to find a particular position wastes time. Both arrays and lists can be resized, but lists more efficiently support insertion and deletion in the middle.

κ΅¬ν˜„μ„ μœ„ν•΄ 선택할 수 μžˆλŠ” λ°©λ²•μœΌλ‘œλŠ” λ°°μ—΄, λͺ©λ‘, λΉ„νŠΈλ§΅, ν•΄μ‹œ ν…Œμ΄λΈ” 등이 μžˆμŠ΅λ‹ˆλ‹€. 배열이 κ°€μž₯ κ°„λ‹¨ν•œ 방법인 κ²½μš°κ°€ λ§Žμ§€λ§Œ, 밀도가 ν¬λ°•ν•œ 배열은 λ©”λͺ¨λ¦¬λ₯Ό λ‚­λΉ„ν•©λ‹ˆλ‹€. listsλŠ” κ°„λ‹¨ν•˜μ§€λ§Œ νŠΉμ • μœ„μΉ˜λ₯Ό μ°ΎκΈ° μœ„ν•΄ κΈ΄ λͺ©λ‘μ„ μ΄λ™ν•˜λŠ” 것은 μ‹œκ°„μ„ λ‚­λΉ„ν•©λ‹ˆλ‹€. λ°°μ—΄κ³Ό lists은 λͺ¨λ‘ 크기λ₯Ό μ‘°μ •ν•  수 μžˆμ§€λ§Œ lists은 쀑간에 μ‚½μž…κ³Ό μ‚­μ œλ₯Ό 더 효율적으둜 μ§€μ›ν•©λ‹ˆλ‹€.

Pintos includes a bitmap data structure in lib/kernel/bitmap.c and include/lib/kernel/bitmap.h. A bitmap is an array of bits, each of which can be true or false. Bitmaps are typically used to track usage in a set of (identical) resources: if resource n is in use, then bit n of the bitmap is true. Pintos bitmaps are fixed in size, although you could extend their implementation to support resizing.

pintOS λŠ” λΉ„νŠΈλ§΅ 데이터 ꡬ쑰λ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€. λΉ„νŠΈλ§΅μ€ λΉ„νŠΈλ“€μ˜ λ°°μ—΄λ‘œ, 각각 μ°Έμ΄κ±°λ‚˜ 거짓일 수 μžˆμŠ΅λ‹ˆλ‹€. λΉ„νŠΈλ§΅λ“€μ€ 일반적으둜 μžμ›λ“€μ˜ μ§‘ν•©μ—μ„œ μ‚¬μš©μ„ μΆ”μ ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ”λ°, μžμ› n이 μ‚¬μš© 쀑이면 λΉ„νŠΈλ§΅μ˜ λΉ„νŠΈ n은 μ°Έμž…λ‹ˆλ‹€. ν•€ν† μŠ€ λΉ„νŠΈλ§΅λ“€μ€ 크기가 κ³ μ •λ˜μ–΄ μžˆμ§€λ§Œ, re-sizing을 μ§€μ›ν•˜λ„λ‘ κ΅¬ν˜„μ„ ν™•μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Pintos also includes a hash table data structure (See Hash Table). Pintos hash tables efficiently support insertions and deletions over a wide range of table sizes.

ν•€ν† μŠ€λŠ” ν•΄μ‹œ ν…Œμ΄λΈ” 재료 ꡬ쑰도 ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€(ν•΄μ‹œ ν…Œμ΄λΈ” μ°Έμ‘°). ν•€ν† μŠ€ ν•΄μ‹œ ν…Œμ΄λΈ”μ€ λ‹€μ–‘ν•œ ν…Œμ΄λΈ” ν¬κΈ°μ—μ„œ μ‚½μž…κ³Ό μ‚­μ œλ₯Ό 효율적으둜 μ§€μ›ν•©λ‹ˆλ‹€.

🎁 Managing the Supplemental Page Table

The supplemental page table supplements the page table with additional data about each page. It is needed because of the limitations imposed by the page table's format. Such a data structure is often called a "page table" also; we add the word "supplemental" to reduce confusion.

보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ” 은 각 νŽ˜μ΄μ§€μ— λŒ€ν•œ μΆ”κ°€ λ°μ΄ν„°λ‘œ νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ„ λ³΄μ™„ν•©λ‹ˆλ‹€. νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ˜ ν˜•μ‹μ— λ”°λ₯Έ μ œν•œ λ•Œλ¬Έμ— ν•„μš”ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ 데이터 κ΅¬μ‘°λŠ” μ’…μ’… "page table"이라고도 λΆˆλ¦¬λŠ”λ°, μš°λ¦¬λŠ” ν˜Όλž€μ„ 쀄이기 μœ„ν•΄ "보쑰"μ΄λΌλŠ” 단어λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
Supplemental Page Table은 ν”„λ‘œμ„ΈμŠ€λ§ˆλ‹€ μ‘΄μž¬ν•˜λŠ” μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

β†’ 각각의 νŽ˜μ΄μ§€μ— λŒ€ν•΄μ„œ 데이터가 μ‘΄μž¬ν•˜λŠ” κ³³(frame, disk, swap 쀑 어디에 μ‘΄μž¬ν•˜λŠ”μ§€), 이에 μƒμ‘ν•˜λŠ” 컀널 κ°€μƒμ£Όμ†Œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터 정보, active인지 inactive 인지 λ“±

The supplemental page table is used for at least two purposes. Most importantly, on a page fault, the kernel looks up the virtual page that faulted in the supplemental page table to find out what data should be there. Second, the kernel consults the supplemental page table when a process terminates, to decide what resources to free.

보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ€ 적어도 두 κ°€μ§€ λͺ©μ μ„ μœ„ν•΄ μ‚¬μš©λ©λ‹ˆλ‹€. κ°€μž₯ μ€‘μš”ν•œ 것은page fault 에 λŒ€ν•΄ 컀널이 보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ—μ„œ fault κ°€ λ°œμƒν•œ 가상 νŽ˜μ΄μ§€λ₯Ό κ²€μƒ‰ν•˜μ—¬ μ–΄λ–€ 데이터가 μžˆμ–΄μ•Ό ν•˜λŠ”μ§€ ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. λ‘˜μ§Έ, 컀널은 ν”„λ‘œμ„ΈμŠ€κ°€ μ’…λ£Œλ  λ•Œ 보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ„ μ‘°μ‚¬ν•˜μ—¬ μ–΄λ–€ λ¦¬μ†ŒμŠ€λ₯Ό free ν•  것인지 κ²°μ •ν•©λ‹ˆλ‹€.

🧹 Organization of Supplemental Page Table

You may organize the supplemental page table as you wish. There are at least two basic approaches to its organization: in terms of segments or in terms of pages. A segment here refers to a consecutive group of pages, i.e., memory region containing an executable or a memory-mapped file.

μš°λ¦¬κ°€ μ›ν•˜λŠ” λŒ€λ‘œ 보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ„ ꡬ성할 수 μžˆμŠ΅λ‹ˆλ‹€. 보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ꡬ성에 λŒ€ν•œ μ΅œμ†Œν•œ 두 κ°€μ§€ 기본적인 접근법이 μžˆμŠ΅λ‹ˆλ‹€: μ„Έκ·Έλ¨ΌνŠΈ μΈ‘λ©΄ λ˜λŠ” νŽ˜μ΄μ§€ μΈ‘λ©΄.
μ—¬κΈ°μ„œ μ„Έκ·Έλ¨ΌνŠΈλŠ” 연속적인 νŽ˜μ΄μ§€ κ·Έλ£Ή, 즉 μ‹€ν–‰ 파일 λ˜λŠ” λ©”λͺ¨λ¦¬ λ§€ν•‘λœ νŒŒμΌμ„ ν¬ν•¨ν•˜λŠ” λ©”λͺ¨λ¦¬ μ˜μ—­μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

Optionally, you may use the page table itself to track the members of the supplemental page table. You will have to modify the Pintos page table implementation in threads/mmu.c to do so. We recommend this approach for advanced students only.

"advanced students only"

❌ Handling page fault

The most important user of the supplemental page table is the page fault handler. In project 2, a page fault always indicated a bug in the kernel or a user program. In project 3, this is no longer true. Now, a page fault might only indicate that the page must be brought in from a file or swap slot. You will have to implement a more sophisticated page fault handler to handle these cases. The page fault handler, which is page_fault() in userprog/exception.c, calls your page fault handler, vm_try_handle_fault() in vm/vm.c.

보쑰 νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ—μ„œ κ°€μž₯ μ€‘μš”ν•œ 것은 page fault handler μž…λ‹ˆλ‹€. project 2μ—μ„œ page fault λŠ” 항상 μ»€λ„μ΄λ‚˜ μ‚¬μš©μž ν”„λ‘œκ·Έλž¨μ˜ 버그λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. ν•˜μ§€λ§Œ Project 3μ—μ„œλŠ” μ•„λ‹™λ‹ˆλ‹€. ν˜„μž¬ ν”„λ‘œμ νŠΈμ—μ„œλŠ” page fault κ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ₯Ό νŒŒμΌμ΄λ‚˜ μŠ€μ™‘ μŠ¬λ‘―μ—μ„œ 가져와야 ν•œλ‹€λŠ” κ²ƒλ§Œ λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ 경우λ₯Ό μ²˜λ¦¬ν•˜λ €λ©΄ 보닀 μ •κ΅ν•œ page fault handlerλ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•©λ‹ˆλ‹€. userprog/exception.cμ—μ„œ page_fault()인 page fault handlerλŠ” vm/vm.cμ—μ„œ 우리의 μ½”λ“œμ˜ page fault handler인 vm_try_handle_fault()λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.

νŽ˜μ΄μ§€ 폴트 ν•Έλ“€λŸ¬λŠ” λŒ€λž΅ λ‹€μŒ μž‘μ—…μ„ μˆ˜ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€:

  1. Supplemental Page Tableμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•œ νŽ˜μ΄μ§€λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€: λ©”λͺ¨λ¦¬ μ°Έμ‘°κ°€ μœ νš¨ν•œ 경우, Supplemental Page Table ν•­λͺ©μ„ μ‚¬μš©ν•˜μ—¬ ν•΄λ‹Ή νŽ˜μ΄μ§€μ— λ“€μ–΄κ°€λŠ” 데이터λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€. λ°μ΄ν„°λŠ” 파일 μ‹œμŠ€ν…œμ΄λ‚˜ μŠ€μ™‘ μŠ¬λ‘―μ— μžˆμ„ 수 있으며, λ‹¨μˆœνžˆ all-zero page 일 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. Copy-on-Writeλ₯Ό κ΅¬ν˜„ν•˜λŠ” 경우, νŽ˜μ΄μ§€μ˜ λ°μ΄ν„°λŠ” 이미 νŽ˜μ΄μ§€ ν”„λ ˆμž„μ— μžˆμ„ 수 μžˆμ§€λ§Œ νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ—λŠ” 없을 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. Supplemental Page Tableμ—μ„œ μ‚¬μš©μž ν”„λ‘œμ„ΈμŠ€κ°€ μ ‘κ·Όν•˜λ €λŠ” μ£Όμ†Œμ— 데이터가 μžˆμ„ κ²ƒμœΌλ‘œ κΈ°λŒ€ν•΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€. νŽ˜μ΄μ§€κ°€ 컀널 가상 λ©”λͺ¨λ¦¬ 내에 μžˆκ±°λ‚˜, 읽기 μ „μš© νŽ˜μ΄μ§€μ— μ“°κΈ°λ₯Ό μ‹œλ„ν•˜λŠ” 경우, 접근이 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 잘λͺ»λœ 접근은 ν”„λ‘œμ„ΈμŠ€λ₯Ό μ’…λ£Œν•˜μ—¬ λͺ¨λ“  λ¦¬μ†ŒμŠ€λ₯Ό ν•΄μ œν•©λ‹ˆλ‹€.

  2. νŽ˜μ΄μ§€λ₯Ό μ €μž₯ν•  ν”„λ ˆμž„μ„ μ–»μŠ΅λ‹ˆλ‹€: Copy-on-Writeλ₯Ό κ΅¬ν˜„ν•œ 경우 ν•„μš”ν•œ 데이터가 이미 ν”„λ ˆμž„μ— μžˆμ„ 수 있으며, 이 경우 ν•΄λ‹Ή ν”„λ ˆμž„μ„ μ°Ύμ•„μ•Ό ν•©λ‹ˆλ‹€.

  3. 데이터λ₯Ό ν”„λ ˆμž„μœΌλ‘œ κ°€μ Έμ˜΅λ‹ˆλ‹€: 파일 μ‹œμŠ€ν…œμ—μ„œ μ½μ–΄μ˜€κ±°λ‚˜ μŠ€μ™‘, zeroing 등을 톡해 데이터λ₯Ό ν”„λ ˆμž„μœΌλ‘œ κ°€μ Έμ˜΅λ‹ˆλ‹€. Copy-on-Writeλ₯Ό κ΅¬ν˜„ν•œ 경우 ν•„μš”ν•œ νŽ˜μ΄μ§€κ°€ 이미 ν”„λ ˆμž„μ— μžˆμ„ 수 있으며, 이 경우 이 λ‹¨κ³„μ—μ„œ μΆ”κ°€ μ‘°μΉ˜κ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  4. νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ν•­λͺ©μ„ μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€: ν΄νŠΈκ°€ λ°œμƒν•œ 가상 μ£Όμ†Œμ˜ νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ν•­λͺ©μ„ 물리 νŽ˜μ΄μ§€λ‘œ λ§€ν•‘ν•©λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ threads/mmu.c의 κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.


πŸ–ΌοΈ Managing the Frame Table

The frame table contains one entry for each frame. Each entry in the frame table contains a pointer to the page, if any, that currently occupies it, and other data of your choice. The frame table allows Pintos to efficiently implement an eviction policy, by choosing a page to evict when no frames are free.

Frame Tableμ—λŠ” 각 Frame에 λŒ€ν•΄ ν•˜λ‚˜μ˜ ν•­λͺ©μ΄ ν¬ν•¨λ©λ‹ˆλ‹€. Frame Table의 각 ν•­λͺ©μ—λŠ” ν˜„μž¬ ν•΄λ‹Ή Frame을 μ μœ ν•˜κ³  μžˆλŠ” Page에 λŒ€ν•œ 포인터와 기타 데이터가 ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. Frame Table을 톡해 PintosλŠ” Frame이 λΆ€μ‘±ν•  λ•Œ ν‡΄μΆœν•  Pageλ₯Ό μ„ νƒν•˜μ—¬ 효율적인 ν‡΄μΆœ 정책을 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

The frames used for user pages should be obtained from the "user pool," by calling palloc_get_page(PAL_USER). You must use PAL_USER to avoid allocating from the "kernel pool," which could cause some test cases to fail unexpectedly. If you modify palloc.c as part of your frame table implementation, be sure to retain the distinction between the two pools.

μ‚¬μš©μž νŽ˜μ΄μ§€μ— μ‚¬μš©λ˜λŠ” Frame은 user poolμ—μ„œ μ–»μ–΄μ•Ό ν•˜λ©°, 이λ₯Ό μœ„ν•΄ palloc_get_page(PAL_USER)λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•©λ‹ˆλ‹€. PAL_USERλ₯Ό μ‚¬μš©ν•˜μ—¬ kernel poolμ—μ„œ ν• λ‹Ήν•˜λŠ” 것을 ν”Όν•΄μ•Ό ν•©λ‹ˆλ‹€. κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ 일뢀 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€κ°€ 예기치 μ•Šκ²Œ μ‹€νŒ¨ν•  수 μžˆμŠ΅λ‹ˆλ‹€. Frame Table κ΅¬ν˜„μ˜ μΌν™˜μœΌλ‘œ palloc.cλ₯Ό μˆ˜μ •ν•˜λŠ” 경우, 두 ν’€ κ°„μ˜ ꡬ뢄을 λ°˜λ“œμ‹œ μœ μ§€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

The most important operation on the frame table is obtaining an unused frame. This is easy when a frame is free. When none is free, a frame must be made free by evicting some page from its frame.

Frame Tableμ—μ„œ κ°€μž₯ μ€‘μš”ν•œ μž‘μ—…μ€ μ‚¬μš©λ˜μ§€ μ•Šμ€ Frame을 μ–»λŠ” κ²ƒμž…λ‹ˆλ‹€. Frame이 λΉ„μ–΄ μžˆμ„ λ•ŒλŠ” 이 μž‘μ—…μ΄ μ‰½μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ λΉ„μ–΄ μžˆλŠ” Frame이 없을 경우, μ–΄λ–€ Pageλ₯Ό ν‡΄μΆœν•˜μ—¬ Frame을 λΉ„μ›Œμ•Ό ν•©λ‹ˆλ‹€.

If no frame can be evicted without allocating a swap slot, but swap is full, panic the kernel. Real OS apply a wide range of policies to recover from or prevent such situations, but these policies are beyond the scope of this project.

μ–΄λ–€ Frame도 μŠ€μ™‘ μŠ¬λ‘―μ„ ν• λ‹Ήν•˜μ§€ μ•Šκ³  ν‡΄μΆœν•  수 μ—†κ³ , μŠ€μ™‘μ΄ 가득 μ°¬ 경우, 컀널을 νŒ¨λ‹‰ μƒνƒœλ‘œ λ§Œλ“œμ‹­μ‹œμ˜€. μ‹€μ œ μš΄μ˜μ²΄μ œλŠ” μ΄λŸ¬ν•œ μƒν™©μ—μ„œ λ³΅κ΅¬ν•˜κ±°λ‚˜ λ°©μ§€ν•˜κΈ° μœ„ν•΄ λ‹€μ–‘ν•œ 정책을 μ μš©ν•˜μ§€λ§Œ, μ΄λŸ¬ν•œ 정책은 이 ν”„λ‘œμ νŠΈμ˜ λ²”μœ„λ₯Ό λ²—μ–΄λ‚©λ‹ˆλ‹€.

ν‡΄μΆœ 과정은 λŒ€λž΅ λ‹€μŒ λ‹¨κ³„λ‘œ κ΅¬μ„±λ©λ‹ˆλ‹€

  1. νŽ˜μ΄μ§€ ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ ν‡΄μΆœν•  Frame을 μ„ νƒν•©λ‹ˆλ‹€. μ•„λž˜μ— μ„€λͺ…λœ λŒ€λ‘œ, νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ˜ "accessed" λΉ„νŠΈμ™€ "dirty" λΉ„νŠΈκ°€ μœ μš©ν•  κ²ƒμž…λ‹ˆλ‹€.
  2. ν•΄λ‹Ή Frame을 μ°Έμ‘°ν•˜λŠ” λͺ¨λ“  νŽ˜μ΄μ§€ ν…Œμ΄λΈ”μ—μ„œ μ°Έμ‘°λ₯Ό μ œκ±°ν•©λ‹ˆλ‹€. 곡유λ₯Ό κ΅¬ν˜„ν•˜μ§€ μ•Šμ€ 경우, μ–΄λŠ μ‹œμ μ—μ„œλ“  ν•˜λ‚˜μ˜ Page만이 Frame을 μ°Έμ‘°ν•΄μ•Ό ν•©λ‹ˆλ‹€.
  3. ν•„μš”ν•œ 경우, νŽ˜μ΄μ§€λ₯Ό 파일 μ‹œμŠ€ν…œμ΄λ‚˜ μŠ€μ™‘μ— κΈ°λ‘ν•©λ‹ˆλ‹€. ν‡΄μΆœλœ Frame은 λ‹€λ₯Έ Pageλ₯Ό μ €μž₯ν•˜λŠ” 데 μ‚¬μš©λ  수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ—‘οΈ Accessed and Dirty Bits

x86-64 hardware provides some assistance for implementing page replacement algorithms, through a pair of bits in the page table entry (PTE) for each page. On any read or write to a page, the CPU sets the accessed bit to 1 in the page's PTE, and on any write, the CPU sets the dirty bit to 1. The CPU never resets these bits to 0, but the OS may do so.

x86-64 ν•˜λ“œμ›¨μ–΄λŠ” 각 νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ν•­λͺ©(PTE)에 μžˆλŠ” 두 개의 λΉ„νŠΈλ₯Ό 톡해 νŽ˜μ΄μ§€ ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ„ κ΅¬ν˜„ν•˜λŠ” 데 도움을 μ€λ‹ˆλ‹€. νŽ˜μ΄μ§€μ— λŒ€ν•œ 읽기 λ˜λŠ” μ“°κΈ°κ°€ λ°œμƒν•  λ•Œλ§ˆλ‹€ CPUλŠ” νŽ˜μ΄μ§€μ˜ PTEμ—μ„œ accessed λΉ„νŠΈλ₯Ό 1둜 μ„€μ •ν•˜κ³ , μ“°κΈ°κ°€ λ°œμƒν•  λ•Œλ§ˆλ‹€ CPUλŠ” dirty bitλ₯Ό 1둜 μ„€μ •ν•©λ‹ˆλ‹€. CPUλŠ” μ΄λŸ¬ν•œ λΉ„νŠΈλ₯Ό 0으둜 μž¬μ„€μ •ν•˜μ§€ μ•Šμ§€λ§Œ, μš΄μ˜μ²΄μ œκ°€ 이λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

You need to be aware of aliases, that is, two (or more) pages that refer to the same frame. When an aliased frame is accessed, the accessed and dirty bits are updated in only one page table entry (the one for the page used for access). The accessed and dirty bits for the other aliases are not updated.

Aliases라고 ν•˜λŠ” 것에 μ£Όμ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. 이것은 두 개 μ΄μƒμ˜ νŽ˜μ΄μ§€κ°€ λ™μΌν•œ ν”„λ ˆμž„μ„ μ°Έμ‘°ν•˜λŠ” 경우λ₯Ό λ§ν•©λ‹ˆλ‹€. Aliased ν”„λ ˆμž„μ— μ•‘μ„ΈμŠ€ν•  λ•Œ, μ•‘μ„ΈμŠ€λœ νŽ˜μ΄μ§€μ— λŒ€ν•΄μ„œλ§Œ νŽ˜μ΄μ§€ ν…Œμ΄λΈ” ν•­λͺ©(PTE)μ—μ„œ accessed 및 dirty λΉ„νŠΈκ°€ μ—…λ°μ΄νŠΈλ©λ‹ˆλ‹€. λ‹€λ₯Έ 별칭에 λŒ€ν•œ accessed 및 dirty λΉ„νŠΈλŠ” μ—…λ°μ΄νŠΈλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

In Pintos, every user virtual page is aliased to its kernel virtual page. You must manage these aliases somehow. For example, your code could check and update the accessed and dirty bits for both addresses. Alternatively, the kernel could avoid the problem by only accessing user data through the user virtual address.

Pintos μ—μ„œλŠ” λͺ¨λ“  μ‚¬μš©μž 가상 νŽ˜μ΄μ§€κ°€ ν•΄λ‹Ήν•˜λŠ” 컀널 가상 νŽ˜μ΄μ§€μ— λ³„μΉ­μœΌλ‘œ μ§€μ •λ©λ‹ˆλ‹€. 이 별칭을 μ–΄λ–»κ²Œ 관리할지 κ²°μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, μ½”λ“œμ—μ„œλŠ” 각 μ£Όμ†Œμ˜ access 및 dirty λΉ„νŠΈλ₯Ό ν™•μΈν•˜κ³  μ—…λ°μ΄νŠΈν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ˜λŠ” 컀널은 μ‚¬μš©μž 데이터에 λŒ€ν•΄μ„œλ§Œ μ‚¬μš©μž 가상 μ£Όμ†Œλ₯Ό 톡해 μ•‘μ„ΈμŠ€ν•˜μ—¬ 이 문제λ₯Ό νšŒν”Όν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

Other aliases should only arise if you implement sharing or if there is a bug in your code.
See Section Page Table Accessed and Dirty Bits for details of the functions to work with accessed and dirty bits.

πŸš₯ Managing the Swap Table

The swap table tracks in-use and free swap slots. It should allow picking an unused swap slot for evicting a page from its frame to the swap partition. It should allow freeing a swap slot when its page is read back or the process whose page was swapped is terminated.

μŠ€μ™‘ ν…Œμ΄λΈ”μ€ μ‚¬μš© 쀑인 μŠ€μ™‘ 슬둯과 빈 μŠ€μ™‘ μŠ¬λ‘―μ„ μΆ”μ ν•©λ‹ˆλ‹€. μ΄λŠ” νŽ˜μ΄μ§€λ₯Ό ν•΄λ‹Ή ν”„λ ˆμž„μ—μ„œ μŠ€μ™‘ νŒŒν‹°μ…˜μœΌλ‘œ ν‡΄μΆœν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜μ§€ μ•Šμ€ μŠ€μ™‘ μŠ¬λ‘―μ„ 선택할 수 μžˆμ–΄μ•Ό 함을 μ˜λ―Έν•©λ‹ˆλ‹€. νŽ˜μ΄μ§€κ°€ λ‹€μ‹œ μ½ν˜”κ±°λ‚˜ νŽ˜μ΄μ§€λ₯Ό μŠ€μ™‘ν•œ ν”„λ‘œμ„ΈμŠ€κ°€ μ’…λ£Œλ  λ•Œ μŠ€μ™‘ μŠ¬λ‘―μ„ freeν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

From the vm/build directory, use the command pintos-mkdisk swap.dsk --swap-size=n to create a disk named swap.dsk that contains a n-MB swap partition. Afterward, swap.dsk will automatically be attached as an extra disk when you run pintos. Alternatively, you can tell pintos to use a temporary n-MB swap disk for a single run with --swap-size=n.

vm/build λ””λ ‰ν† λ¦¬μ—μ„œ pintos-mkdisk swap.dsk --swap-size=n λͺ…령을 μ‚¬μš©ν•˜μ—¬ n-MB μŠ€μ™‘ νŒŒν‹°μ…˜μ„ ν¬ν•¨ν•˜λŠ” swap.dskλΌλŠ” λ””μŠ€ν¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€. 이후에 swap.dskλŠ” pintosλ₯Ό μ‹€ν–‰ν•  λ•Œ μžλ™μœΌλ‘œ μΆ”κ°€ λ””μŠ€ν¬λ‘œ μ—°κ²°λ©λ‹ˆλ‹€. λ˜λŠ” --swap-size=n을 μ‚¬μš©ν•˜μ—¬ 단일 싀행에 λŒ€ν•œ μž„μ‹œ n-MB μŠ€μ™‘ λ””μŠ€ν¬λ₯Ό μ‚¬μš©ν•˜λ„λ‘ pintos에 μ§€μ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Swap slots should be allocated lazily, that is, only when they are actually required by eviction. Reading data pages from the executable and writing them to swap immediately at process startup is not lazy. Swap slots should not be reserved to store particular pages.
Free a swap slot when its contents are read back into a frame.

μŠ€μ™‘ μŠ¬λ‘―μ€ lazy ν•˜κ²Œ ν• λ‹Ήλ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. 이 말은, eviction에 μ‹€μ œλ‘œ ν•„μš”ν•  λ•Œλ§Œ ν• λ‹Ήλ˜μ–΄μ•Ό ν•œλ‹€λŠ” λ§μž…λ‹ˆλ‹€. ν”„λ‘œμ„ΈμŠ€κ°€ μ‹œμž‘λ  λ•Œ μ‹€ν–‰νŒŒμΌμ—μ„œ 데이터 νŽ˜μ΄μ§€λ“€μ„ 읽고 μŠ€μ™‘μ— κ³§λ°”λ‘œ μ“°λŠ” ν–‰μœ„λŠ” lazy ν•˜μ§€ λͺ»ν•œ ν–‰μœ„μž…λ‹ˆλ‹€. νŠΉμ • νŽ˜μ΄μ§€λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•΄ μŠ€μ™‘ 슬둯이 μ˜ˆμ•½λ˜μ–΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€.
μŠ€μ™‘ 슬둯의 λ‚΄μš©λ¬Όμ΄ ν”„λ ˆμž„μœΌλ‘œ μ½ν˜€ λŒμ•„μ˜€λ©΄ κ·Έ λ•Œ μŠ€μ™‘ μŠ¬λ‘―μ„ free ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€.

πŸ“‚ Managing Memory Mapped Files

The file system is most commonly accessed with read and write system calls. A secondary interface is to "map" the file into virtual pages, using the mmap system call. The program can then use memory instructions directly on the file data. Suppose file foo is 0x1000 bytes (4 kB, or one page) long. If foo is mapped into memory starting at address 0x5000, then any memory accesses to locations 0x5000...0x5fff will access the corresponding bytes of foo.

파일 μ‹œμŠ€ν…œμ€ 주둜 read와 write μ‹œμŠ€ν…œ ν˜ΈμΆœμ„ μ‚¬μš©ν•˜μ—¬ μ•‘μ„ΈμŠ€λ©λ‹ˆλ‹€. 보쑰 μΈν„°νŽ˜μ΄μŠ€λ‘œλŠ” νŒŒμΌμ„ 가상 νŽ˜μ΄μ§€λ‘œ "λ§€ν•‘"ν•˜λŠ” 것 μž…λ‹ˆλ‹€. μ΄λŠ” mmap μ‹œμŠ€ν…œ ν˜ΈμΆœμ„ μ‚¬μš©ν•©λ‹ˆλ‹€. ν”„λ‘œκ·Έλž¨μ€ 그런 λ‹€μŒ 파일 데이터에 λŒ€ν•΄ 직접 λ©”λͺ¨λ¦¬ λͺ…령을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, 파일 fooκ°€ 0x1000 λ°”μ΄νŠΈ(4KB λ˜λŠ” ν•œ νŽ˜μ΄μ§€) 길이인 κ²½μš°μž…λ‹ˆλ‹€. 파일 fooκ°€ μ£Όμ†Œ 0x5000μ—μ„œ λ©”λͺ¨λ¦¬μ— λ§€ν•‘λ˜μ–΄ μžˆλ‹€κ³  κ°€μ •ν•΄λ³΄μ„Έμš”. 그럼 μ£Όμ†Œ 0x5000λΆ€ν„° 0x5fffκΉŒμ§€μ˜ λͺ¨λ“  λ©”λͺ¨λ¦¬ μ•‘μ„ΈμŠ€λŠ” foo의 ν•΄λ‹Ή λ°”μ΄νŠΈμ— μ•‘μ„ΈμŠ€ν•˜κ²Œ λ©λ‹ˆλ‹€.

#include <stdio.h>
#include <syscall.h>

int main (int argc UNUSED, char *argv[])
{
  void *data = (void *) 0x10000000;                 /* Address at which to map. */
  int fd = open (argv[1]);                          /* Open file. */
  void *map = mmap (data, filesize (fd), 0, fd, 0); /* Map file. */
  write (1, data, filesize (fd));                   /* Write file to console. */
  munmap (map);                                     /* Unmap file (optional). */
  return 0;
}

πŸ”– Sitation πŸ“

  1. κΆŒμ˜μ§„(2022). "Virtual Memory.pptx"
  2. 원문 좜처: https://casys-kaist.github.io/pintos-kaist/appendix/virtual_address.html

'PintOS' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[PintOS - VM] Anonymous Page  (2) 2024.06.07
[PintOS - VM] Memory Management  (1) 2024.06.07
[PintOS - Userprog] Exec  (0) 2024.05.30
[PintOS - Userprog] Wait  (0) 2024.05.29
[PintOS - Userprog] Fork, Exec, Wait 에 λ“€μ–΄κ°€κΈ° μ•žμ„œ...  (0) 2024.05.29