Moderate

xv6 book Sect 5.5 Real world: Memory-mapped files

File-backed Memory Mapping

As discussed in Chapter 3 of the xv6 book, the kernel maps physical pages into a process’s virtual address space using page table entries. So far, new pages are always zero-initialized. A useful extension is to initialise a page’s contents from a file instead. In this exercise you will extend xv6’s mmap(vaddr, perms) syscall (which you implemented in Lab 4) to accept an optional file descriptor: mmap(uint64 vaddr, uint perms, int fd). When fd is -1, we keep the existing semantics where mmap returns a new page filled with zeroes. When fd refers to a valid, readable file, the new page is filled with up to one page of file data; the remainder is zeroed.

:bulb: We restrict ourselves to read-only files and at most one page to keep the implementation minimal.

Part 1: Updating the syscall signature

Change the user-space declaration in user/user.h from

int mmap(uint64 vaddr, uint perms);

to

int mmap(uint64 vaddr, uint perms, int fd);

Update any existing mmap test programs accordingly. To preserve backwards compatibility, callers that do not want a file-backed mapping should pass fd = -1.

Part 2: Validating the file

In sys_mmap in kernel/sysproc.c, after parsing va and perms, call

argfd(2, 0, &f);

to retrieve a pointer to the struct file (or NULL if fd is -1). You will need to add #include "kernel/file.h" at the top of sysproc.c for the struct file type to be in scope.

If a file was provided, verify the file refers to an inode (f->type == FD_INODE) and is read-only and not writable before proceeding. Return -1 if any condition fails.

:question: What could go wrong if a writable file were accepted here?

Part 3: Filling the page

After mappages succeeds, read from the file using fileread into the newly mapped virtual address if a valid file descriptor where provided. Then zero the bytes that were not filled with memset.

:bulb: Click to show hint

Use a MIN macro to avoid reading past the end of the file or past the end of the page:

#define MIN(a, b) ((a) < (b) ? (a) : (b))

int remaining = MIN(f->ip->size - f->off, PGSIZE);

int nread = fileread(f, va, remaining);

Part 4: Testing

Add the following program as user/mmapfd.c and add it to the Makefile:

#include "kernel/fcntl.h"
#include "kernel/riscv.h"
#include "user/user.h"
#include "kernel/memlayout.h"
#include "kernel/param.h"

#define BASE  0x0000000baddad000
#define PERMS (PTE_R | PTE_W)

int main(int argc, char **argv)
{
  int fd;
  char *page = (char*) BASE;

  if ((fd = open("README", O_RDONLY)) < 0) {
    fprintf(2, "cannot open file\n");
    exit(1);
  }

  if (mmap(BASE, PERMS, fd) < 0) {
    fprintf(2, "mmap() failed\n");
    exit(1);
  }

  for (int i = 0; i < PGSIZE; i++)
    printf("%c", page[i]);
  printf("\n");

  return 0;
}

Run mmapfd and verify that its output matches cat README (up to one page). Then check the following edge cases:

  • Pass fd = -1: mmap should succeed and the page should be all zeroes.
  • Open README with O_RDWR and pass it: mmap should return -1.
  • Map to a virtual address that is already mapped: mmap should fail the second time.

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