首页 > 其他 > 详细

[OS][MIT 6.828] Lab 2: Memory Management

时间:2020-11-29 23:41:05      阅读:65      评论:0      收藏:0      [点我收藏+]

Lab link: https://pdos.csail.mit.edu/6.828/2018/labs/lab2/

Introduction

Memory management has two components:

  1. a physical memory allocator for the kernel, so that the kernel can allocate memory and later free it.
  2. virtual memory, which maps the virtual addresses used by kernel and user software to addresses in physical memory. The x86 hardware‘s memory management unit (MMU) performs the mapping when instructions use memory, consulting a set of page tables.

Some critical variables in JOS:

struct PageInfo *pages;             // whole physical memory
struct PageInfo *page_free_list;    // free physical memory
pde_t           *kern_pgdir;        // whole virtual memory space

1 PageInfo struct represents 1 physical page, 1 pte represents 1 page.
given 1 va, you can determine whether it has been mapped by looking at whether the corresponding pte present or not.

Part 1: Physical Page Management

The operating system keep track of which parts of physical RAM are free and which are currently in use. JOS manages the PC‘s physical memory with page granularity.

This part implememts the physical page allocator. It keeps track of which pages are free with a linked list of struct PageInfo objects, each corresponding to a physical page.

// If n>0, allocates enough pages of CONTIGUOUS physical memory to hold ‘n‘ bytes.
// This simple physical memory allocator is used only while JOS is setting up its virtual memory system.  page_alloc() is the real allocator. So this function is 
// static.
// This function may ONLY be used during initialization, before the **page_free_list** list has been set up.
static void * boot_alloc(uint32_t n)

// Initialize page structure and memory free list. After this is done, boot_alloc() will NEVER be used again.
void page_init(void)

// Allocates a physical page. Does NOT increment the reference count of the page.
struct PageInfo * page_alloc(int alloc_flags)

// Return a page to the free list.
void page_free(struct PageInfo *pp)

Part 2: Virtual Memory

Address translation scheme:

技术分享图片
// returns a pointer to the PTE for linear address ‘va‘.
pte_t * pgdir_walk(pde_t *pgdir, const void *va, int create)

// Map [va, va+size) of virtual address space to physical [pa, pa+size) in the page table rooted at pgdir.
static void boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm)

// Unmaps the physical page at virtual address ‘va‘.
void page_remove(pde_t *pgdir, void *va)

// Map the physical page ‘pp‘ at virtual address ‘va‘.
int page_insert(pde_t *pgdir, struct PageInfo *pp, void *va, int perm)

Part 3: Kernel Address Space

[OS][MIT 6.828] Lab 2: Memory Management

原文:https://www.cnblogs.com/-zyq/p/13417424.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!