pml4 allocate & activate

load 함수 내에서 page directory를 activate 하는 부분

/* Allocate and activate page directory. */
	t->pml4 = pml4_create();
	if (t->pml4 == NULL)
		goto done;
	process_activate(thread_current());

t->pml4 = pml4_create();

핀토스 부팅 과정에서 cr3 레지스터에는 base_pml4 주소 저장되는데, pml4_create을 통해 pml4는 base_pml4 내용을 복사해서 갖게 된다.

process_activate(thread_current()); : 현재 스레드의 pml4를 인자로 pml4_activate() 함수를 실행킴으로서 현재 스레드의 pml4를 cr3 레지스터로 올려준다


file 읽어서 메모리에 로드

CODE, DATA, STACK순으로 하나하나 load시켜준다. 이 하나하나가 segment라고 불리우고, 이것을 load시키는 함수가 바로 load_segment함수이다.

		if (validate_segment(&phdr, file))
			{
				bool writable = (phdr.p_flags & PF_W) != 0;
				uint64_t file_page = phdr.p_offset & ~PGMASK;
				uint64_t mem_page = phdr.p_vaddr & ~PGMASK;
				uint64_t page_offset = phdr.p_vaddr & PGMASK;
				uint32_t read_bytes, zero_bytes;
				if (phdr.p_filesz > 0)
				{
					/* Normal segment.
					 * Read initial part from disk and zero the rest. */
					read_bytes = page_offset + phdr.p_filesz;
					zero_bytes = (ROUND_UP(page_offset + phdr.p_memsz, PGSIZE) - read_bytes);
				}
				else
				{
					/* Entirely zero.
					 * Don't read anything from disk. */
					read_bytes = 0;
					zero_bytes = ROUND_UP(page_offset + phdr.p_memsz, PGSIZE);
				}
				if (!load_segment(file, file_page, (void *)mem_page,
								  read_bytes, zero_bytes, writable))
					goto done;
			}

uint64_t file_page = phdr.p_offset & ~PGMASK; : file_page를 통해서 file의 위치를 가져오고,