Moderate
xv6 book Sect 5.1 Lazy allocation
xv6 book Sect 5.2 Code
Copy-on-Write Null Page
As discussed in Chapter 5 of the xv6 book, lazy allocation defers physical memory allocation until a page is actually accessed. This is especially useful when memory is scarce or when programs use only a fraction of their allocated address space, avoiding work for pages that are never touched.
In this exercise, you will extend xv6’s lazy allocation with a common optimization used in real-world operating systems. Make sure you understand how lazy allocation is implemented in xv6. If you don’t fully understand this, read Section 5.2 again.
The key observation is that heap memory is typically initialized to zero and often only partially modified. To exploit this, you will implement a copy-on-write (COW) null page: a single, read-only physical page filled with zeros that is mapped to all lazily allocated heap regions. This allows processes to read zero-initialized memory without allocating separate physical pages.
Recall from Section 5.1 that by calling sbrk with the SBRK_LAZY flag, pages in xv6 are normally allocated on first access. Here, instead, all such virtual memory will initially map to the shared zero page. A write to this page triggers a page fault, at which point the kernel allocates a new private page, remaps it with write permissions, and resumes execution.
This approach reduces memory usage and avoids unnecessary allocations while preserving correct behavior.
Part 1: Allocating the null page
First, make sure that xv6 allocates a single null_page on startup that is never freed, as it can be used by all processes.
- Define a global
char *null_pagepointer inkalloc.cinitialized to0(i.e., null pointer). - Add
extern char *null_page;todefs.hto declarenull_pageas a symbol that is defined. This way you can refer to this symbol in every file that includesdefs.h. - Modify
kinitto allocate a single physical page filled with all-zeroes, and assign its address to thenull_pagepointer.
Click to show hintUse
kallocandmemset. - Make sure this
null_pageis never freed by xv6 (e.g., upon process termination when all physical pages belonging to the terminated process are freed).
Click to show hintYou should only modify
kfree.
Part 2: Mapping the null page
Next, make sure that the null page is mapped in the virtual address space of a process when the process calls sbrk with the SBRK_LAZY flag.
- Create a function with signature
uint64 uvmallocnull_page(pagetable_t pagetable, uint64 oldsz, uint64 newsz)invm.c, similar to theuvmallocfunction. This should map the physicalnull_pageto all virtual pages betweenoldszandnewszwith read-only permissions.
Click to show hintStart from a copy of the
uvmalloccode. - Modify
sys_sbrkso that it usesuvmallocnull_pagewhensbrkis invoked with theSBRK_LAZYflag.
Part 3: Copy on write
Lastly, ensure that the read-only null page is properly replaced when a write occurs. As described in Section 5.2, the vmfault function in vm.c handles page faults triggered by writes to lazily allocated pages. In the current implementation, this function returns an error if the virtual address va lies outside the region previously allocated via sbrk, or if the page is already mapped.
The latter behavior must be revised. Instead of returning an error when the page is already mapped (i.e., the null page), the handler should first unmap the null page, then allocate and map a new zero-initialized page with write permissions.
Part 4: Testing your implementation
You can test your implementation by using the following user-space program.
Since this program uses malloc to dynamically allocate memory for five pages, you will need to modify the file user/umalloc.c to ensure that the malloc implementation calls sbrk with the SBRK_LAZY flag. This can be done by replacing the call to sbrk with sbrklazy.
#include "user.h"
#include "kernel/riscv.h" // for PGSIZE
#define SIZEOF_ARRAY (5 * PGSIZE)
int main(void)
{
// Allocate SIZEOF_ARRAY bytes
printf("-- Allocating %u bytes", SIZEOF_ARRAY);
char *bytes = malloc(SIZEOF_ARRAY);
if (bytes == 0) {
fprintf(2, "\nmalloc() failed\n");
}
printf(" at (virtual) address %p\n", bytes);
vmprintmappings();
// Read first byte of first page of allocated memory
// -> must be zero!
printf("-- Reading byte 0 at %p: value=%d\n", bytes, bytes[0]);
vmprintmappings();
// Write to the first page of the allocated memory
// -> a new page must be allocated!
printf("-- Writing byte at %p\n", &bytes[0*PGSIZE]);
bytes[0*PGSIZE] = 0x00;
vmprintmappings();
// Write to the third page of allocated memory
// -> a new page must be allocated!
printf("-- Writing byte at %p\n", &bytes[2*PGSIZE]);
bytes[2*PGSIZE] = 0x00;
vmprintmappings();
return 0;
}