Foundations Level: Beginner Chapter 1: How Windows Works Underneath

Windows Layers and System Calls

Windows is built in layers, and a program never touches hardware directly. It asks through a system call, and the kernel does the privileged work.

Lesson 1 of 5 in Chapter 1: How Windows Works Underneath. This is the first lesson in this chapter. 3 learning objectives, 4 examples, and a knowledge check.

Start: what you will learn

Windows is built in layers, and a program never touches hardware directly. It asks through a system call, and the kernel does the privileged work.

Learning objectives

  • Name the layers of Windows from apps down to hardware
  • Explain what a system call is and where NTDLL fits
  • Explain why PowerShell runs in user mode and asks the system through APIs

Concept in simple words

What it is: Windows is built in layers, like floors in a building. At the top are your apps, such as PowerShell and Notepad. Below them are subsystem DLLs, which are shared code libraries that many apps use for common jobs. Below that is NTDLL, a special library that acts as the gate between user mode and kernel mode. User mode is where normal apps run, with limited power. Kernel mode is where the core of Windows and its drivers run, with full power over the machine.

Why it works

Why it works this way: This split keeps one bad app from breaking the whole machine. If an app crashes in user mode, Windows can usually close that app and keep going. If code crashes in kernel mode, the whole system can stop, because kernel code can touch everything. So Windows only lets trusted core code and drivers into kernel mode, and it makes normal apps ask politely instead of grabbing hardware themselves.

Think of it like this

Think of it like this: User mode is the public lobby of a building, and kernel mode is the locked engine room. NTDLL is the reception desk between them. An app walks up to the desk with a request, the desk passes it through in the correct form, and the engine room does the work and sends the answer back. The app never walks into the engine room itself.

How it works, step by step

How it works, step by step: First, your app asks for something, such as reading a file. Next, shared code in a subsystem DLL prepares that request. Then NTDLL turns it into a system call, which is a formal request to the kernel, and the system switches into kernel mode. Then the Executive, which is the main part of the kernel, uses its managers to do the job, for example the I/O manager talks to a driver, and the HAL, which is the Hardware Abstraction Layer, translates the request for your exact hardware. Finally, the result travels back up the same layers to your app.

Let us walk through an example: When you run Get-Process in PowerShell, PowerShell itself runs in user mode. It does not read the list of processes straight from hardware or memory. It asks Windows through an API, which is a set of ready made functions that programs use to ask the system for things. Windows checks the request, gathers the process facts in kernel mode, and hands them back. PowerShell then shows you a table, and that table is the friendly result of a much deeper journey.

When to use it

When to use it, and when not to: Use this picture whenever a command behaves in a way you did not expect, and ask which layer could be the cause. Is the app wrong, is access denied by the kernel, or is a driver or hardware problem involved. Do not try to bypass the layers, and do not run code that claims to touch hardware directly, because that is not how normal Windows apps work, and it is a common sign of something unsafe.

Start from zero: A layer is a group of code that hides detail from the layer above it. The app above does not need to know which disk you own, which CPU model you own, or how the hardware talks. It only needs to know the request it can make, and the answer it will get back. That hiding is called abstraction, and it is the whole point of layers. Without it, every app would need its own code for every piece of hardware, and that would be slow, fragile, and unsafe.

The stack in order, top to bottom: First comes your app, such as PowerShell. Next come subsystem DLLs, which hold shared functions for common jobs, such as opening files and creating processes. Next comes NTDLL, the gate to the kernel. Next comes the Executive, the main kernel code, with managers for different jobs. Next come drivers, which know how to talk to one kind of device. Next comes the HAL, which hides CPU and hardware differences. Last comes the hardware itself. If you can walk down that list in order, you can place almost any Windows problem in the right layer.

User mode and kernel mode are about processor privilege, not about speed or importance. In kernel mode, code can run privileged CPU instructions and can touch any memory and any device. In user mode, code cannot run those instructions, and it cannot touch memory that belongs to another process or to the kernel. If user mode code tries, the processor stops it, and Windows turns that attempt into an error for the app. That hardware help is what makes the split real, and not just a polite rule.

A system call in slow motion: First, the app calls a normal API function, such as a function to open a file. Next, the subsystem DLL checks the simple parts of the request and prepares it. Next, NTDLL issues the system call, and the CPU switches from user mode to kernel mode. Next, the kernel checks the request again, more strictly. It checks that pointers point to memory the caller is allowed to use, and that the caller has permission for the job. Next, the kernel managers and drivers do the work. Last, the result and a status code travel back up, and the app continues. A bad request usually returns an error, not a system crash, because the kernel checked it first.

The managers inside the Executive each own one kind of job. The I/O manager owns input and output, and it talks to drivers for disks, printers, and other devices. The memory manager owns virtual memory, pages, and how those pages map to real RAM. The process and thread manager owns creating processes, creating threads, and tracking them. The object manager owns kernel objects, such as files and processes, and the handles that refer to them. You do not need to memorise every manager, but it helps to know that the kernel is not one giant block. It is a team of managers, each with a clear job.

A second picture: Think of ordering food in a restaurant. You do not walk into the kitchen and cook it yourself. You tell the waiter what you want, the waiter writes it in kitchen language, the kitchen cooks it, and the waiter brings it back. The API is the menu, the subsystem DLL is the waiter writing the order, NTDLL is the kitchen door, and the kernel and drivers are the kitchen. If the kitchen is out of an ingredient, the waiter does not pretend the dish exists. The waiter brings back a clear message, and that is like an error code from the kernel.

Walk through reading a file, layer by layer: First, PowerShell asks to read a file, through an API. Next, the subsystem DLL prepares the request. Next, NTDLL turns it into a system call. Next, the I/O manager in the kernel receives it, and asks the file system driver to find the file on disk. Next, that driver asks the disk driver to read the right blocks. Next, the HAL translates the request for your exact hardware. Next, the disk returns the data, and the answer travels back up, layer by layer, until PowerShell can show it. If one layer fails, the layers above it get an error, and they cannot simply skip the failed layer.

What goes wrong, and where to look: If the app has a bug, the request itself is wrong, and fixing the app is the answer. If permission is missing, the kernel says access is denied, and that is a permission problem, not a broken app. If a driver is faulty, the job can fail even when the app and the permission are both fine, and the clue is often that one device or one kind of job fails while other jobs work. If hardware fails, the lowest layer cannot deliver, and the clue is often that the same job fails in every app. Ask which layer stopped first, and you will waste far less time guessing.

Two edge cases worth knowing: First, a 32 bit app on 64 bit Windows gets an extra compatibility layer, which translates its requests, but it still ends at the same NTDLL gate and the same kernel. The extra layer changes the shape of the request, not the rule that apps must ask. Second, there is no simple read only cmdlet in this lesson that shows you each system call live as it happens. Do not look for one here, and do not trust a guide that invents one. Get-Process shows you the result after the journey, namely the running processes. It does not show you the journey itself, and that distinction matters.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
Get-Process

Example 1

Get-Process asks Windows for the running processes. The pipe, written as |, passes those process objects to the next command. Select-Object keeps only the Name and Id properties, and -First 5 keeps only the first five results, so you get a short readable list.

Example 1
Get-Process | Select-Object Name, Id -First 5

Illustrative output: Name Id ---- -- explorer 1234 powershell 5678 notepad 9012

Example 2

Get-Process asks Windows for the running processes. Sort-Object orders those objects by their Id property, from lowest to highest. Select-Object then keeps only the Name and Id properties, and -First 3 keeps only the first three results.

Example 2
Get-Process | Sort-Object Id | Select-Object Name, Id -First 3

Illustrative output: Name Id ---- -- System 4 Registry 120 smss 456

Example 3

Get-Process asks Windows for the running processes. The -Name parameter narrows the result at the source to processes called explorer, and you type the name without .exe. The pipe, written as |, passes those process objects to the next command. Select-Object then keeps only the Name and Id properties. Look for one or more explorer rows, and look at the Id to tell them apart. If you change the name to a process that is not running, you get an error, not an empty table, so check the spelling and check that the process is really running.

Example 3
Get-Process -Name explorer | Select-Object Name, Id

Illustrative output: Name Id ---- -- explorer 1234

Example 4

Get-Process asks Windows for the running processes. Sort-Object orders those objects by their CPU property, and -Descending flips the order from highest to lowest, so the largest values come first. Select-Object then keeps the Name, Id, and CPU properties, and -First 3 keeps only the first three results. Look for the largest CPU values at the top, and remember that CPU here is total processor time used so far, not a live percent. If you remove -Descending, the smallest values come first instead, and your top 3 list would show the quietest processes, which is a very different answer.

Example 4
Get-Process | Sort-Object CPU -Descending | Select-Object Name, Id, CPU -First 3

Illustrative output: Name Id CPU ---- -- --- explorer 1234 12.5 powershell 5678 3.2

Nothing on this page executes PowerShell code. The outputs above are illustrative only.

Common mistakes and troubleshooting

Read these before you try the examples, so you can spot the same problems in your own work.

  • Thinking PowerShell talks to hardware directly, when it runs in user mode and asks Windows through APIs.
  • Thinking a crash in one app means the whole system is broken, when user mode crashes are usually contained to that app.
  • Thinking NTDLL is just another app library, when it is the gate that turns a request into a system call into kernel mode.
  • Reading the CPU column as a live percent, when CPU in Get-Process output is total processor time used so far, not how busy the process is right now.
  • Thinking -First 5 gives the five most important results, when it only gives the first five results in the current order, so sort first if order matters to you.
  • Thinking Select-Object changes the processes themselves, when it only makes new smaller objects for display, and the running processes stay exactly as they were.

Knowledge check

Knowledge check

What job does the HAL do in the layers of Windows?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: Apps sit on top, subsystem DLLs share common code, NTDLL is the syscall gate, the Executive managers run the kernel, the HAL talks to hardware, and PowerShell stays in user mode and asks the system for everything it needs.

Next step: Processes and Threads