Advanced OS

Table of Contents

User mode

Privilege separation

Operating system’s task:

Privilege separation on x86:

Security

Meltdown (rogue data cache load):

Foreshadow (L1TF)

MDS (RIDL):

Defending kernel attacks:

Interrupts

Most software interrupts are synchronous: directly before/after instruction

Most hardware interrupts are asynchronous: can come at any time, proper masking is important

During an interrupt:

  1. CPU elevates privilege level and switches to kernel stack
  2. Some user context (e.g. rip) is saved
  3. function is called to handle interrupt (“interrupt service routine”)
    • on x86, interrupt descriptor table (IDT) shows how to handle various interrupts
    • IDTR register points to IDT (set with lidt instruction)
    • IDT has max 256 entries (1 byte int)
    • first 32 entries are exceptions
    • 16 external interrupts can be remapped using APIC unit
    • calling interrupt handler:
      • interrupt vector used as inex into IDT, which has interrupt gates
        • type of gate can be interrupt or trap
        • interrupt gate clears IF in EFLAGS to mask further interrupts
      • jump to interrupt handler: set CS to segment selector (changing ring), set RIP to offset (jumps to interrupt handler)
      • switch stack:
        • kernel stack pointer stored in Task State Segment (TSS)
        • task register (TR) contains index in the GDT that specifies where TSS is (can set with ltr instruction)
        • glue pieces of base address together to find address of TSS
        • TSS contains stack pointers for each ring
        • load RSP0 from TSS into RSP
        • set stack segment to null
      • store calling context
        • old register values stored on kernel stack, to be restored later
    • returning from interrupts
      • iret returns back to location at time of interrupts
      • pops right amount from stack, restores stack, returns to last rip, drops privilege
  4. CPU restores some of user context and drops privilege level

Dealing with livelocks (when the CPU is doing work, but not useful work):

System calls

Kernel support for servicing user apps.

Originally only issues using int instruction. Kernel-user communication dictated by calling convention.

Each OS specifies its own calling convention.

syscall

VDSO: