A Guide to coremap.c
Overview
The coremap is the heart of physical-memory management in OS/161. It maintains information about every physical page (a.k.a. frame) of memory in the system through an array of coremap_entry structures. The coremap handles both allocation requests and page-replacement decisions.
Key Data Structures
Coremap Entry
struct coremap_entry {
struct lpage *cm_lpage; /* logical page we hold, or NULL */
volatile
int cm_tlbix:7; /* tlb index number, or -1 */
unsigned cm_cpunum:5; /* cpu number for cm_tlbix */
unsigned cm_kernel:1, /* true if kernel page */
cm_notlast:1, /* true not last in sequence of kernel pages */
cm_allocated:1; /* true if page in use (user or kernel) */
volatile
unsigned cm_pinned:1; /* true if page is busy */
/* other fields as needed */
};
Important fields to understand:
cm_lpage: Points to the logical page currently using this physical pagecm_kernel: Distinguishes kernel pages (which can't be evicted) from user pagescm_allocated: Indicates if the page is in usecm_pinned: Indicates the page is busy (e.g., being read from disk)
Key Functions You'll Interact With
The header file kern/arch/mips/include/coremap.h contains the coremap interface and is worth reading over. The implementation is in kern/arch/mips/vm/coremap.c.
Page-Replacement Interface
/*
* page_replace: Choose a page to evict.
* Returns: index into coremap of chosen page
*
* This is where you'll implement both clock and random algorithms
*/
static
uint32_t
page_replace(void)
{
/* Your implementation goes here */
}
TLB Invalidation
The function tlb_invalidate is used to invalidate a TLB entry. The TLB only has a small number of entries, so sometimes pages are removed from the TLB to make room for others. Being removed from the TLB does not necessarily mean the page is evicted from memory.
Important Helper Functions
/* Check if a page is pinned */
int coremap_pageispinned(paddr_t paddr);
/* Convert between physical addresses and coremap indices */
#define COREMAP_TO_PADDR(i) (((paddr_t)PAGE_SIZE)*((i)+base_coremap_page))
#define PADDR_TO_COREMAP(page) (((page)/PAGE_SIZE) - base_coremap_page)
Key Operations and Their Flow
Page Allocation
static
paddr_t
coremap_alloc_one_page(struct lpage *lp, int dopin)
{
/* ... */
if (num_coremap_free == 0) {
/* No free pages - calls page_replace() to choose victim */
candidate = do_page_replace();
}
/* ... */
}
Page Replacement
When memory is full and a new page is needed,
coremap_alloc_one_page()detects no free pages- Calls
do_page_replace() do_page_replace()callspage_replace()to choose a victim- Selected page is evicted if necessary
- Page is allocated to new use
Implementation Hints
For Page-Replacement Algorithms
- The clock algorithm needs to track page usage. Consider:
- What information needs to be stored per page?
- How will you update this information?
- When should information be updated?
- Random replacement needs to
- Use the
random()function appropriately - Ensure chosen pages are valid candidates
- Handle cases where chosen page isn't suitable
- Use the
- Both algorithms must
- Never choose kernel pages (
cm_kernel == true) - Never choose pinned pages (
cm_pinned == true) - Keep trying until finding a suitable page
- Never choose kernel pages (
Common Pitfalls
- Synchronization
- The coremap spinlock protects the coremap
- Be careful about lock ordering
- Watch for deadlocks
- Page Selection
- Always verify page can be evicted
- Handle case where all pages are pinned
- Consider efficiency (especially for clock algorithm)
- State Management
- Track algorithm state correctly
- Handle wraparound properly
- Consider all edge cases
Debugging Tips
-
Use
coremap_print_short()to visualize memory stateKindicates kernel page*indicates allocated user page.indicates free page&indicates pinned page
-
Common issues:
- Infinite loops in page selection
- Selecting invalid pages
- Missing synchronization
- Memory leaks
Testing Strategy
- Test basic functionality:
- Does algorithm select valid pages?
- Does it avoid kernel pages?
- Does it handle pinned pages?
- Test edge cases:
- Nearly full memory
- Many pinned pages
- All pages pinned
- Multiple consecutive allocations
- Test efficiency:
- For clock algorithm: Is it using history effectively?
- For random: Is distribution reasonable?
Additional Notes
- The coremap is initialized during boot in
coremap_bootstrap() - Page replacement only happens for user pages
- Understanding the relationship between physical addresses and coremap entries is crucial
- Remember to handle the case where no pages can be evicted
Remember: Good page-replacement algorithms balance efficiency (quick decisions) with effectiveness (good choices). The clock algorithm should perform better than random replacement in most cases, but both need to be robust and reliable.
(When logged in, completion status appears here.)