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.

  1. Define a global char *null_page pointer in kalloc.c initialized to 0 (i.e., null pointer).
  2. Add extern char *null_page; to defs.h to declare null_page as a symbol that is defined. This way you can refer to this symbol in every file that includes defs.h.
  3. Modify kinit to allocate a single physical page filled with all-zeroes, and assign its address to the null_page pointer.
    :bulb: Click to show hint

    Use kalloc and memset.

  4. Make sure this null_page is never freed by xv6 (e.g., upon process termination when all physical pages belonging to the terminated process are freed).
    :bulb: Click to show hint

    You 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.

  1. Create a function with signature uint64 uvmallocnull_page(pagetable_t pagetable, uint64 oldsz, uint64 newsz) in vm.c, similar to the uvmalloc function. This should map the physical null_page to all virtual pages between oldsz and newsz with read-only permissions.
    :bulb: Click to show hint

    Start from a copy of the uvmalloc code.

  2. Modify sys_sbrk so that it uses uvmallocnull_page when sbrk is invoked with the SBRK_LAZY flag.

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;
}

This site uses Just the Docs, a documentation theme for Jekyll.