欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

WSL(Windows Subsystem for Linux)--Pico Process Overview

程序员文章站 2024-01-14 09:20:04
【转载】 Windows Subsystem for Linux -- Pico Process Overview Overview This post discusses pico processes, the foundation of WSL. It explains how pico pro ......

【转载】

windows subsystem for linux -- pico process overview

overview

this post discusses pico processes, the foundation of wsl.  it explains how pico processes work in windows and goes into the history of how they came to be, the abstractions we decided to implement and the various use cases beyond wsl that emerged. armed with this context, the next series of posts will dive into specific areas of how exactly wsl works.

drawbridge

the pico process concept originated in msr as part of the drawbridge project. a goal of this project was to implement a lightweight way to run an application in an isolated environment, with the application’s os dependencies decoupled from the underlying host os (e.g., xp application running on windows 10).  normally, this would be done by running the application and os in a virtual machine, but this comes with significant resource overhead. instead, the drawbridge project aimed to run the target application and os entirely within the user-mode address space of a single process on the host os. with its reduced overhead, when compared to a vm, this approach allows for greater density of application workloads on a single host while still providing much of the same isolation and compatibility guarantees. check out the official drawbridge project page for more detailed information for how they pulled this off.

in drawbridge, the “library os” was the target os of the application workload.  to support a library os that differs from the underlying host os in addition to running the library os in user-mode, the msr folks needed the host os to get out of the way and stop trying to manage the user-mode address space inside this process. they coined this type of process a “pico process” on the host side to indicate that it is a smaller version of a normal host process. a kernel-mode driver was responsible for supporting the pico process and acting as the broker between the host os kernel and the library os in user-mode.

of course, proper support for this on top of a publicly released and supported version of windows would require core windows kernel changes to add the facilities necessary for pico processes. the msr team brought this idea to us over in the kernel team toward the beginning of 2013, and we all agreed it would be great feature to add. in fact, we realized that this approach aligned well with some internal discussion we had been having around longer-term strategy to bring back the idea of subsystems to facilitate future architectural changes within windows. this initial support for pico processes first appeared in windows 8.1 and windows server 2012r2 but was limited to drawbridge. pico process support was later expanded to other windows features.

minimal process

as we started implementing official support for pico processes, we decided to split the abstraction into two layers:

  • minimal process: this is the most rudimentary type of process. specifically, a process marked as a minimal process tells the rest of the host to get out of the way and not manage it. from the windows kernel point of view, it is simply an empty user-mode address space.
  • pico process: this is a minimal process with an associated pico provider kernel-mode driver to manage that empty user-mode address space.

WSL(Windows Subsystem for Linux)--Pico Process Overview

unlike traditional nt processes, when creating a minimal process, the user-mode address space is untouched and no threads are created to run in that process. various locations in the kernel were surgically updated to skip user-mode address space setup, including:

  • the user-mode binary ntdll.dll is not mapped into the process by default.
  • the process environment block (peb) is not created.
  • there is no initial thread created, and thread environment blocks (tebs) are not automatically created whenever a thread is created for the pico process.
  • the shared user data section is not mapped into the process. this is a block of memory mapped read-only into all user-mode address space for efficient retrieval of common system-wide information.
  • various places that assumed a process would always have a peb and/or tebs were updated to be able to handle processes without them.

while the windows kernel does not actively manage a minimal process, it still provides all of the underlying os support you would expect – thread scheduling, memory management, etc.

you may be wondering – why did we separate the notion of “minimal” and “pico” processes? the idea of an empty minimal process seemed useful on its own, separate from anything related to supporting a pico process. while we had nothing specific in mind around this time back in 2013, eventually a couple of scenarios did surface in windows 10 that are now using minimal processes directly:

  • memory compression: memory compression is a windows feature that compresses unused memory to keep more data resident in ram. it also reduces the amount of data written to and read from the pagefile, thus improving performance. windows memory compression utilizes the user-mode address space of a minimal process.
  • virtualization based security (vbs): using underlying virtualization capabilities, vbs isolates the user-mode address space of critical user-mode processes from the rest of the os to prevent tampering, even from the kernel or kernel-mode drivers. a minimal process is created to indicate to management tools (e.g., task manager) that vbs is running.

pico processes and providers

a pico process is simply a minimal process that is associated with a pico provider kernel-mode driver. this pico provider surfaces the entire kernel interface as far as the user-mode portion of the process is concerned. the windows kernel passes all system calls and exceptions that originate from the user-mode portion of a pico process to the pico provider to handle as it sees fit. this allows the pico provider to model a different user/kernel contract separate from what windows would normally provide.

early during boot, a kernel-mode driver registers with the windows kernel as a pico provider, and the kernel and provider exchange a set of interfaces specific to the needs of a pico provider. for example, the pico provider provides function pointers for the kernel to call when dispatching a user-mode system call or exception, and the kernel provides function pointers for creating pico processes and threads.

regardless of what behaviors and abstractions the pico provider exposes to user-mode, it ultimately will rely on the windows kernel for underlying support of thread scheduling, memory management and i/o. of course, portions of the windows kernel had to be updated to support new scenarios where needed.

windows kernel changes

later posts will detail the windows kernel changes in more detail, but here is a quick sampling:

  • improved fork support: yes – the windows kernel has supported “fork” for a long time (going back to earlier posix and sfu application support), but it is not exposed in the win32 programming model that the rest of windows is programmed against. we have improved the fork implementation to meet some new requirements as part of the wsl work.
  • fine-grained memory management: windows normally manages the user-mode address space in 64kb chunks, but was updated to allow management at single-page 4kb granularity for pico processes.
  • case-sensitive file names: again, yes – the windows kernel and ntfs have long supported case-sensitive file names, but it is disabled by default and not exposed in the win32 programming model. changes were made to allow individual threads to opt-in to case-sensitivity operations to support a broader range of wsl scenarios.