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.
-
Instruct the compiler to place the
ticksvariable in a section named.vdso. The GCC compiler provides thesectionattribute to achieve this as follows:uint ticks __attribute__((section(".vdso")));By applying the section attribute only to the
ticksvariable this way, a dedicated.vdsosection is created in the object file that will contain only theticksvariable. -
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"); }
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
.vdsooutput section that:- Starts and ends on a page boundary (a multiple of 4096 (0x1000) bytes).
- Occupies exactly one page (4096 bytes) when loaded into memory.
- Contains all variables associated with
__attribute__((section(".vdso")))(in our case, only theticksvariable). - Defines a symbol named
_vdso_startat the start of the .vdso section.
This is a good point to verify that the kernel is still functioning correctly.
To ensure the kernel gets compiled properly after these changes in the linker script, run make cleanbefore runningmake qemu.
Assignment
- Map the page containing the
.vdsosection in each user process.- 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_startis both a valid virtual and physical address. -
Extend the function
proc_pagetable, which sets up the initial mappings of a process, to also map the.vdsosection. Use virtual addressTRAPFRAME - PGSIZEfor 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.
- Don’t forget to unmap the page again when the process is terminated.
- You can get a pointer to the start of the .vdso section with the following code:
- Implement a user-space library function named
fastuptimethat returns the value of theticksvariable without calling into the kernel. If you followed the previous steps correctly, theticksvariable will be located at the beginning of the user-space page that maps to the.vdsosection. - To test your implementation, write a user-space program called
uptimewhich calls both theuptimesystem call (wrapper) and your custom user-space library functionfastuptime. Do the two calls return the same value each time you execute it in the xv6 shell?
If you need a refresher on how to implement a user-space library function, refer to the puts exercise from the first lab.