2

The following quote is from the "Understanding the Linux Kernel 3rd Edition" book:

When a User Mode process attempts to access an I/O port by means of an in or out instruction, the CPU may need to access an I/O Permission Bitmap stored in the TSS to verify whether the process is allowed to address the port.

More precisely, when a process executes an in or out I/O instruction in User Mode, the control unit performs the following operations:

  1. It checks the 2-bit IOPL field in the eflags register. If it is set to 3, the control unit executes the I/O instructions. Otherwise, it performs the next check.

  2. It accesses the tr register to determine the current TSS, and thus the proper I/O Permission Bitmap.

  3. It checks the bit of the I/O Permission Bitmap corresponding to the I/O port specified in the I/O instruction. If it is cleared, the instruction is executed; otherwise, the control unit raises a “General protection” exception.

The following quote is also from the same book:

Although Linux doesn’t use hardware context switches, it is nonetheless forced to set up a TSS for each distinct CPU in the system.

Now if Linux only has one TSS structure for all processes (instead of each process having its own TSS structure), and we know that each process must have its own I/O Permission Bitmap, does that mean that when Linux schedule the execution to another process, Linux would change the value of the I/O Permission Bitmap in the only TSS structure the CPU uses to the value of the I/O Permission Bitmap of the process to be executed (which Linux presumably stores somewhere in kernel memory)?

Hadi Brais
  • 22,259
  • 3
  • 54
  • 95
Tom
  • 1,344
  • 9
  • 27
  • I haven't looked at the Linux kernel in ages (regarding its software task switching), but at one time it use to physically copy the IO bitmap of the next task (if the IO Bitmap Offset indicated a presence of an IO bitmap) into the TSS. If no IO bitmap was present in the next task (the default in the absence of one is to deny all port access) then nothing is copied and the IO bitmap offset is set to something that indicates no bitmap.This all use to be part of the software context switching mechanism. Each process had its own bitmap (or no bitmap) – Michael Petch Apr 03 '19 at 18:54
  • PS: There is one TSS per processor. On a multicore system there is a TSS for each processor in use. Each processor can of course be running multiple processes. – Michael Petch Apr 03 '19 at 19:01

1 Answers1

3

Yes. From the same section of the book, it says:

The tss_struct structure describes the format of the TSS. As already mentioned in Chapter 2, the init_tss array stores one TSS for each CPU on the system. At each process switch, the kernel updates some fields of the TSS so that the corresponding CPU’s control unit may safely retrieve the information it needs. Thus, the TSS reflects the privilege of the current process on the CPU, but there is no need to maintain TSSs for processes when they’re not running.

In later versions of the kernel, init_tss was renamed to cpu_tss. The TSS structure of each processor is initialized in cpu_init, which is executed once per processor when booting the system.

When switching from one task to another, __switch_to_xtra is called, which calls switch_to_bitmap, which simply copies the IO bitmap of the next task into the TSS structure of the processor on which it is scheduled to run next.

Related: How do Intel CPUs that use the ring bus topology decode and handle port I/O operations.

Hadi Brais
  • 22,259
  • 3
  • 54
  • 95