Microkernel Construction

Similar documents
Microkernel Construction

Tutorial 10 Protection Cont.

Part I. X86 architecture overview. Secure Operating System Design and Implementation x86 architecture. x86 processor modes. X86 architecture overview

IA32 Intel 32-bit Architecture

PROTECTION CHAPTER 4 PROTECTION

Operating Systems Engineering Recitation #3 (part 2): Interrupt and Exception Handling on the x86. (heavily) based on MIT 6.

Mechanisms for entering the system

x86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT

+ Overview. Projects: Developing an OS Kernel for x86. ! Handling Intel Processor Exceptions: the Interrupt Descriptor Table (IDT)

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

W4118: interrupt and system call. Junfeng Yang

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Introduction to The x86 Microprocessor

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

BASIC INTERRUPT PROCESSING

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

iapx Systems Electronic Computers M

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

iapx86 Protection Electronic Computers M

Information Security II Prof. Kamakoti Department of Computer Science and Engineering Indian Institute of Technology, Madras

AMD-K5. Software Development Guide PROCESSOR

6.828: Using Virtual Memory. Adam Belay

MOV Move INSTRUCTION SET REFERENCE, A-M. Description. Opcode Instruction 64-Bit Mode. Compat/ Leg Mode

2.5 Address Space. The IBM 6x86 CPU can directly address 64 KBytes of I/O space and 4 GBytes of physical memory (Figure 2-24).

MICROPROCESSOR ALL IN ONE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

Protection and System Calls. Otto J. Anshus

µ-kernel Construction

W4118: PC Hardware and x86. Junfeng Yang

Basic Execution Environment

Embedded Systems Programming

W4118: virtual machines

What You Need to Know for Project Three. Dave Eckhardt Steve Muckle

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

CSCI 350 Ch. 2 The Kernel Abstraction & Protection. Mark Redekopp

Buffer Overflow Attack

IA32/Linux Virtual Memory Architecture

Threads, System Calls, and Thread Switching

CanSecWest Nicolás A. Economou Andrés Lopez Luksenberg

The Microprocessor and its Architecture

3. Process Management in xv6

Introduction to IA-32. Jo, Heeseung

Darshan Institute of Engineering & Technology

INTRODUCTION TO IA-32. Jo, Heeseung

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

CS3210: Booting and x86

Program Exploitation Intro

x86 architecture et similia

µ-kernel Construction

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

143A: Principles of Operating Systems. Lecture 6: Address translation. Anton Burtsev January, 2017

µ-kernel Construction (12)

CHAPTER 6 INTERRUPT AND EXCEPTION HANDLING

Chapter 2: The Microprocessor and its Architecture

Pre-virtualization internals

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

143A: Principles of Operating Systems. Lecture 5: Address translation. Anton Burtsev October, 2018

Practical Malware Analysis

Operating Systems and Protection CS 217

M80C286 HIGH PERFORMANCE CHMOS MICROPROCESSOR WITH MEMORY MANAGEMENT AND PROTECTION

CS3210: Booting and x86. Taesoo Kim

CS 3803 Lab 6 Interrupts Assigned 2/25/10 Point Value: 30

Interrupts and System Calls

238P: Operating Systems. Lecture 5: Address translation. Anton Burtsev January, 2018

Complex Instruction Set Computer (CISC)

Operating systems offer processes running in User Mode a set of interfaces to interact with hardware devices such as

Windows Kernel Internals Traps, Interrupts, Exceptions

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

CS 550 Operating Systems Spring Interrupt

CHAPTER 3 BASIC EXECUTION ENVIRONMENT

Intel386 TM DX MICROPROCESSOR SPECIFICATION UPDATE

Assembler Programming. Lecture 2

Intel VMX technology

Intel386 DX PROCESSOR SPECIFICATION UPDATE

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Interrupts and System Calls. Don Porter CSE 506

The K Project. Interrupt and Exception Handling. LSE Team. May 14, 2018 EPITA. The K Project. LSE Team. Introduction. Interrupt Descriptor Table

Preliminary Information. AMD Duron Processor Model 7 Revision Guide

Unit 08 Advanced Microprocessor

x86 Assembly Tutorial COS 318: Fall 2017

MILITARY Intel386 TM HIGH PERFORMANCE 32-BIT MICROPROCESSOR WITH INTEGRATED MEMORY MANAGEMENT

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

8/09/2006. µ-kernel Construction. Fundamental Abstractions. Thread Switch A B. Thread Switch A B. user mode A kernel. user mode A

Assembly Language Lab # 9

complex instruction set compute. reduced instruction set compute FETCH&EXECUTE. Instruction Register -

The Instruction Set. Chapter 5

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

CIS Operating Systems CPU Mode. Professor Qiang Zeng Spring 2018

Chapter 12: INTERRUPTS

OS-SOMMELIER: Memory-Only Operating System Fingerprinting in the Cloud

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

Interrupts. Chapter 20 S. Dandamudi. Outline. Exceptions

Computer Organization (II) IA-32 Processor Architecture. Pu-Jen Cheng

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Interrupts & System Calls

Microprocessors and Microcontrollers/High end processors

Assembly Language. Lecture 2 x86 Processor Architecture

Transcription:

Microkernel Construction Kernel Entry / Exit Nils Asmussen 05/04/2017 1 / 45

Outline x86 Details Protection Facilities Interrupts and Exceptions Instructions for Entry/Exit Entering NOVA Leaving NOVA 2 / 45

Privilege Levels Ring 3 Ring 2 Ring 1 Ring 0 Ring 0 Operating System Kernel Privileged Ring 1 & 2 Operating System Services Unprivileged Ring 3 User-level Applications Unprivileged 3 / 45

Protection Facilities Segmentation Mechanism for dividing linear address space into segments Different segment types: code, data, stack,... Segment has base address and limit Mandatory mechanism; cannot be disabled Paging Mechanism for translating linear to physical addresses Splits virt. & phys. address space into same-size pages Per-page access rights (present, writable, user/kernel) Optional; can be turned off (not on x86 64) 4 / 45

Logical Address Translation Logical address consists of Segment selector Segment offset CPU uses table indicator in segment selector to access descriptor in GDT/LDT Segment descriptor provides segment base address, segment limit and access rights (checked by CPU) CPU adds segment offset to segment base address to form linear address 5 / 45

Logical Address Translation Logical Address: Seg. Selector Seg. Offset Limit Seg. Desc + Segment Linear Address GDT Base Address Linear Address Space 6 / 45

Segment Registers CPU provides 6 segment registers for holding selectors CS (code segment) DS (data segment) SS (stack segment) ES, FS, GS (extra data segments) Selector forms visible part Base address, limit and access rights form shadow part 7 / 45

Segment Descriptor 63 47 32 Base Address (31..24) G D 0 A V L Limit (19..16) Type P DPL 1 1 C R A Base Address (23..16) Base Address (15..0) Segment Limit (15..0) 31 15 Figure: Code Segment Descriptor 0 G Granularity DPL Descriptor Privilege Level D Default Size (16/32 Bit) C Conforming AVL Available to Programmer R Readable P Present A Accessed 8 / 45

Flat Memory Model Hide segmentation mechanism by Setting segment base address to 0 Setting segment limit to 4 GiB Mandatory for x86 64 We need at least 2 segments Code segment Data/Stack segment If kernel/user use different segment base addresses or segment limits, we need 2 additional segments for each 9 / 45

Protection Checks CPU checks on instructions and memory references that certain protection conditions hold: Segment limit check Segment type check Privilege level check Restricted procedure entry points Restricted instruction set... CPU raises exception if protection check fails Protection checks in parallel with address translation No big performance penalty 10 / 45

Privileged Instructions LGDT LLDT LTR LIDT MOV to CR LMSW CLTS MOV to DR INVD WBINVD INVLPG HLT RDMSR / WRMSR RDPMC / RDTSC CLI / STI 11 / 45

Interrupts & Exceptions Forced transfer of execution from currently executing code to interrupt- or exception handler Hardware interrupts occur asynchronously in response to hardware events Exceptions (including software interrupts) occur synchronously to the instruction stream 12 / 45

Interrupt & Exception Vectors Vector uniquely identifies source of the interrupt or exception Vectors 0.. 31 are used for exceptions Vectors 32.. 255 are designated user vectors (e.g., interrupts) Interrupt Descriptor Table (IDT) is table of handler functions for all interrupts and exceptions Setup by OS Hardware is informed about it via LIDT instruction Vector = offset into IDT 13 / 45

Interrupt Descriptor Table (IDT) IDTR register: IDT base address IDT limit Gate for Vector n +... Gate for Vector 2 Gate for Vector 1 Gate for Vector 0 14 / 45

Restricted Procedure Entry Points: Gates Descriptors to call procedures at different privilege levels Subject to CPL/RPL vs. DPL checking CS and EIP loaded from descriptor Interrupt Gate disables interrupts, Trap Gate doesn t When switching privilege levels, new ESP loaded from TSS TSS contains stack pointers for privilege levels 0, 1 and 2 Kernel updates SP 0 in TSS on every thread switch Interrupt and exception handlers runs current thread s context 15 / 45

Interrupt Gate 63 47 32 Offset (31..16) P DPL 01110000 Reserv. Segment Selector Offset (0..15) 31 15 0 Selector Offset DPL P Segment Selector for destination code segment Offset to procedure entry point in segment Descriptor privilege level (ignored for HW ints) Segment present flag 16 / 45

Exception Types Faults Can be corrected and allow the program to be resumed Processor restores machine state prior to the execution of faulting instruction (CS and EIP point to faulting instruction) Traps Reported immediately after execution of trapping instruction Allow the program to be resumed (CS and EIP point to following instruction) Aborts Are not always reported at the precise location of the exception Do not allow to resume the program Usually report severe hardware errors or inconsistent state 17 / 45

Exceptions 0: Divide Error (F) 1: Debug Exception (F/T) 2: Non-maskable Interrupt 3: Breakpoint (T) 4: Overflow (T) 5: Bound Range Exc. (F) 6: Invalid Opcode (F) 7: Device Not Available (F) 8: Double Fault (A) 9: Coproc. Seg. Overr. (F) 10: Invalid TSS (F) 11: Segm. Not Present (F) 12: Stack Segment Fault (F) 13: General Prot. Fault (F) 14: Pagefault (F) 15: reserved 16: FPU Math Fault (F) 17: Alignment Check (F) 18: Machine Check (A) 19: SIMD FP Exception (F) 18 / 45

Kernel Stack after Kernel Entry EFLAGS CS EIP SS ESP EFLAGS (Error Code) No Priv. Level Change ESP CS EIP (Error Code) Priv. Level Change 19 / 45

Kernel Entry SS ESP EFLAGS CS EIP entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 20 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 21 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 Error=0 entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 22 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 23 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 24 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt 25 / 45

Kernel Entry SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) entry_6: push $6 jmp entry_exc entry_exc: push $0 entry_exc_err: push %ds push %es push %fs push %gs pusha mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc handler jmp ret_from_interrupt 26 / 45

Exception Handler REGPARM(1) void exc_handler (Exc_regs *r) { switch (r->vec) { case Cpu::EXC_TS: handle_exc_ts (r); break; case Cpu::EXC_PF: handle_exc_pf (r); break;... } } void handle_exc_pf (Exc_regs *r) { mword addr = r->cr2; if(pd::current->space_mem::loc[cpu::id].sync_from ( Pd::current->Space_mem::hpt, addr)) return;... } 27 / 45

Kernel Exit to Kernel Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... )... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret from interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret_user_iret popa add $24, %esp iret 28 / 45

Kernel Exit to Kernel Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... )... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret user iret popa add $24, %esp iret 29 / 45

Kernel Exit to Kernel Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... )... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret_user_iret popa add $24, %esp iret 30 / 45

Kernel Exit to Kernel Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret_user_iret popa add $24, %esp iret 31 / 45

Kernel Exit to Kernel Mode SS ESP EFLAGS CS EIP... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret_user_iret popa add $24, %esp iret 32 / 45

Kernel Exit to Kernel Mode... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret_user_iret popa add $24, %esp iret 33 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... )... mov %esp, %ebx mov $KSTCK_BEGIN, %esp mov %cr2, %eax mov %eax, 0xc(%ebx) mov %ebx, %eax call exc_handler jmp ret_from_interrupt ret_from_interrupt: testb $3, 0x3c(%ebx) jnz ret user iret popa add $24, %esp iret 34 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) void ret_user_iret() { handle hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 35 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 36 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS GPR (eax, ecx, edx, ebx, cr2,... ) void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 37 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 DS ES FS GS void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 38 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP Vector=6 Error=0 void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 39 / 45

Kernel Exit to User Mode SS ESP EFLAGS CS EIP void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 40 / 45

Kernel Exit to User Mode void ret_user_iret() { handle_hazards(); } asm volatile ( "lea %0, %%esp;" "popa;" "pop %%gs;" "pop %%fs;" "pop %%es;" "pop %%ds;" "add $8, %%esp;" "iret;" : : "m" (current->regs) : "memory" ); UNREACHED; 41 / 45

Kernel Exit: IRET Load code segment selector, EIP and flags from stack Evaluate RPL of CS from stack RPL > CPL: return to other privilege level otherwise: return to same privilege level If privilege level changes Load stack segment selector and ESP from stack Adjust CPL 42 / 45

Fast Kernel Entry: SYSENTER Fast ring transition from any ring to ring 0 Kernel code entry point specified via SYSENTER CS MSR (code segment) SYSENTER EIP MSR (entry function) SYSENTER ESP MSR (kernel stack pointer) CPU... Disables interrupts Loads kernel CS (from SYSENTER CS MSR) Loads kernel SS (from SYSENTER CS MSR + 8) Loads kernel ESP (from SYSENTER ESP MSR) Loads kernel EIP (from SYSENTER EIP MSR) Does NOT save user return EIP or other user state 43 / 45

Fast Kernel Exit: SYSEXIT Fast ring transition from any ring to ring 3 CPU... Loads user CS (from SYSENTER CS MSR + 16) Loads user SS (from SYSENTER CS MSR + 24) Loads user ESP (from ECX) Loads user EIP (from EDX) Enables interrupts 44 / 45

Next Weeks May, 11th: Exercise 1 (Kernel entry/exit) in room E067 May, 18th: Exercise 2 (Multiboot, ELF) in room E067 May, 25th: Interprocess communication June, 1st: Exercise 3 (Thread switching) in room E067 June, 8th: Capabilities... 45 / 45