Krafton Jungle/5. PintOS

[PintOS 2주차] Day 1

munsik22 2025. 5. 15. 23:01

핀토스 2-3주차 동안에는 Project 2: User Programs를 구현한다.

 

🔹 프로젝트 목표: OS에서 사용자 프로그램 실행시키기

  • 프로세스와 스레드 생성
  • 프로그램의 VA 설정하기: code, data, stack
  • 실행가능 파일 로드하기
  • 실행파일 시작하기: 인수 전달하기

🔹 2주차 학습 계획표 (예시)

  진행 사항
금-일 - 일정 수립
- 이론 학습 (gitbook: introduction, argument passing / CSAPP: 8.2 - 8.4)
- argument passing 구현
- argument passing 리팩토링
- 이론 학습 (gitbook: system call)
- system call 구현 (halt, file system 관련)
- file system 관련 system call 구현 완료
- wait, exec, fork, exit 구현 시작
금-일 - 테스트 완료
- system call 코드 수정

Virtual Addresses #

64비트의 가상 주소는 다음과 같은 구조를 가진다.

#define PGSHIFT { /* Omit details */ } // Bit index (0) of VA
#define PGBITS { /* Omit details */ } // # of bits (12) of the offset part of VA

#define PGMASK { /* Omit details */ }
// A bit mask with the bits in the page offset set to 1, the rest set to 0 (0xfff)

#define PGSIZE { /* Omit details */ } // The page size in bytes (4,096)

#define pg_ofs(va) { /* Omit details */ } // Extracts and returns the page offset in va.
#define pg_no(va) { /* Omit details */ } // Extracts and returns the page number in va.

#define pg_round_down(va) { /* Omit details */ }
// Returns the start of the virtual page that va points within, that is, va with the page offset set to 0.

#define pg_round_up(va) { /* Omit details */ } // Returns va rounded up to the nearest page boundary.
#define KERN_BASE { /* Omit details */ }
// Base address of kernel virtual memory. It defaults to 0x8004000000. 
// User virtual memory ranges from virtual address 0 up to KERN_BASE. 
// Kernel virtual memory occupies the rest of the virtual address space.
    • 핀토스의 VM은 사용자 VM과 커널 VM으로 나뉘고(참고), 그 둘 사이의 경계가 KERN_BASE이다.
#define is_user_vaddr(vaddr) { /* Omit details */ } // Returns true if va is a user virtual address
#define is_kernel_vaddr(vaddr) { /* Omit details */ } // Returns true if va is a kernel virtual address

 

x86-64는 주어진 PA에 대해 메모리에 직접 접근하는 어떤 방법도 제공하지 않는다. 이러한 기능은 운영 체제 커널에서 종종 필요하기 때문에 Pintos는 커널 VM을 PM과 일대일로 매핑하여 이를 해결한다. 즉, KERN_BASE 보다 큰 VA는 물리 주소 0에 접근하고, VA KERN_BASE + 0x1234는 PA 0x1234에 접근하는 식으로 머신의 물리 메모리 최대 크기까지 접근한다. 따라서 KERN_BASE를 PA에 더하면 해당 주소에 접근하는 커널 VA가 생성되고, 반대로 커널 VA에서 KERN_BASE를 빼면 해당 PA가 생성된다.

#define ptov(paddr) { /* Omit details */ }
// Returns the kernel virtual address corresponding to physical address pa, 
// which should be between 0 and the number of bytes of physical memory.
#define vtop(vaddr) { /* Omit details */ }
// Returns the physical address corresponding to va, which must be a kernel virtual address.
#define is_user_pte(pte) { /* Omit details */ } // Query whether the PTE is owned by user
#define is_kern_pte(pte) { /* Omit details */ } // Query whether the PTE is owned by kernel
#define is_writable(pte) { /* Omit details */ } // Query whether VA pointed by the PTE is wriatable or not

typedef bool pte_for_each_func (uint64_t *pte, void *va, void *aux);
bool pml4_for_each (uint64_t *pml4, pte_for_each_func *func, void *aux);
// For each valid entry under PML4, apply FUNC with auxillary value AUX. 
// VA represents the virtual address of the entry. 
// If pte_for_each_func returns false, stop iteration and return false.
/* Example func that could be fed to pml4_for_each */
static bool stat_page (uint64_t *pte, void *va,  void *aux) {
        if (is_user_vaddr (va))
                printf ("user page: %llx\n", va);
        if (is_writable (va))
                printf ("writable page: %llx\n", va);
        return true;
}

'Krafton Jungle > 5. PintOS' 카테고리의 다른 글

[PintOS 2주차] Day 3  (0) 2025.05.17
[PintOS 2주차] Day 2  (0) 2025.05.16
[PintOS 1주차] Bonus: Advanced Scheduler  (0) 2025.05.15
[PintOS 1주차] Day 8: 마무리  (0) 2025.05.15
[PintOS 1주차] Day 7  (0) 2025.05.14