vDSO: Using kernel functions in user space

Moderate

xv6 book Sect 3.2 Kernel address space

xv6 book Sect 3.3 Code: creating an address space

xv6 book Sect 3.6 Process address space

Look at the implementation of the uptime system call in xv6. Ignoring the acquire and release calls for now (locking will be covered later in the homework assignments), the function simply returns the value of the global variable ticks. A system call, such as uptime, involves two expensive context switches: one from user mode to supervisor mode and another from supervisor mode back to user mode. As a result, retrieving the value of a global variable through a system call, while straightforward, is not the most efficient approach.

In this exercise, we will implement a more efficient approach inspired by the Linux kernel’s virtual dynamic shared object (vDSO) mechanism, which improves performance by leveraging virtual memory. The Linux kernel uses vDSO to handle specific system calls entirely in user space, reducing the need for costly context switches. It aggregates frequently accessed variables into a dedicated page, which is mapped as read-only in each process’s address space. This enables processes to read (but not modify) these variables without the overhead of a system call, significantly improving performance.

We will implement a similar mechanism in xv6 and make the ticks variable accessible from user space.

Allocating the ticks variable on a dedicated page

An xv6 PTE maps a contiguous 4096-byte virtual memory page to a contiguous 4096-byte physical memory frame. Since the ticks variable only occupies 4 bytes (sizeof(uint)), we need to ensure that no other, potentially sensitive, kernel variables are exposed if we map the page containing this variable in user space. We can realize this by placing the ticks variable on a dedicated page that will contain no other data.

  1. Instruct the compiler to place the ticks variable in a section named .vdso. The GCC compiler provides the section attribute to achieve this as follows:

    uint ticks __attribute__((section(".vdso")));
    

    By applying the section attribute only to the ticks variable this way, a dedicated .vdso section is created in the object file that will contain only the ticks variable.

  2. Next, we need to modify the linker script (kernel/kernel.ld). This script provides the linker with instructions on how to combine the kernel’s object files into an executable. Add the following just before the line PROVIDE(end = .);:

    .vdso : {            /* Start an "output section" named .vdso */
      . = ALIGN(0x1000); /* Align the current address (.) on a page boundary */
      _vdso_start = .;   /* Create a symbol _vdso_start and assign it the current address */
      *(.vdso);          /* Put all "input sections" named .vdso in the current "output section" */
      . = ALIGN(0x1000); /* Align the current address on a page boundary */
      ASSERT(. - _vdso_start == 0x1000, "error: vdso section exceeds one page");
    }
    

    :bulb: Don’t be concerned if the above syntax seems confusing; linker scripts can be quite complex. You can refer to the linker manual for more information about linker scripts (you are not expected to study this for the exam!).

    Recompiling the kernel with these changes will produce a kernel executable with a .vdso output section that:

    1. Starts and ends on a page boundary (a multiple of 4096 (0x1000) bytes).
    2. Occupies exactly one page (4096 bytes) when loaded into memory.
    3. Contains all variables associated with __attribute__((section(".vdso"))) (in our case, only the ticks variable).
    4. Defines a symbol named _vdso_start at the start of the .vdso section.

    This is a good point to verify that the kernel is still functioning correctly.

    :warning: To ensure the kernel gets compiled properly after these changes in the linker script, run make clean before running make qemu.

Assignment

  1. Map the page containing the .vdso section in each user process.
    1. You can get a pointer to the start of the .vdso section with the following code:
       extern char _vdso_start[];
      

      Recall from Section 3.2 of the xv6 book that the kernel maps all physical RAM and device registers to virtual addresses equal to their physical addresses, a scheme known as direct mapping. This means that _vdso_start is both a valid virtual and physical address.

    2. Extend the function proc_pagetable, which sets up the initial mappings of a process, to also map the .vdso section. Use virtual address TRAPFRAME - PGSIZE for the mapping, ensure that the page is mapped as read-only, and that it is accessible from user space. For guidance, look at how the trampoline page is mapped at the highest area of the virtual address space. Try to understand on the drawing below where the vDSO page should be in the process’s virtual address space.

      xv6-process-address-space

    3. Don’t forget to unmap the page again when the process is terminated.
  2. Implement a user-space library function named fastuptime that returns the value of the ticks variable without calling into the kernel. If you followed the previous steps correctly, the ticks variable will be located at the beginning of the user-space page that maps to the .vdso section.
  3. To test your implementation, write a user-space program called uptime which calls both the uptime system call (wrapper) and your custom user-space library function fastuptime. Do the two calls return the same value each time you execute it in the xv6 shell?

:bulb: If you need a refresher on how to implement a user-space library function, refer to the puts exercise from the first lab.


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